Write a program to demonstrate the three different ways of copying a list.

 

Answer:

Code:

import copy 

list1=[ 'red' , 'green' , 'blue' , 'yellow' , 'orange'

 

#method 1 

list2= list1 

print (list2) 

 

#method 2 

list3=copy.copy(list1) 

print(list3) 

 

#method 3 

list4=list1[:] 

print(list4)

 

Question 1 Long Answer.jpg

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo