Differentiate between deep and shallow copy in python?



Differentiate between deep and shallow copy in python?..

Answer / Shailesh Kumar Yadav

In Python, a deep copy creates separate copies of all sub-objects, while a shallow copy maintains references to the original sub-objects.nn```pythonnimport copynlist1 = [1, 2, [3, 4]]nlist2 = copy.deepcopy(list1)nlist3 = copy.copy(list1)nlist1[2][0] = 5nprint(list1) # Output: [1, 2, [5, 4]]nprint(list2) # Output: [1, 2, [3, 4]]nprint(list3) # Output: [1, 2, [5, 4]]

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Python Interview Questions

Tell me how memcached should not be used in your python project?

1 Answers  


What is the optional statement used in a try except statement in Python?

1 Answers  


What happens when you execute python == python?

1 Answers  


How python maintains conditional blocks?

1 Answers  


What does the ‘yield’ keyword do?

1 Answers  


How do you calculate the length of a string?

1 Answers  


What is the most popular ide for python?

1 Answers  


Why do array indexes start with 0 in most languages?

1 Answers  


Why is python 2 still used?

1 Answers  


When do you use list vs. tuple vs. dictionary vs. set?

1 Answers  


How do I avoid having python class data shared among instances?

1 Answers  


Is there a switch or case statement in python?

1 Answers  


Categories