Difference between copy and = in list

>>> import copy
>>> a = [1,2,3]
>>> b = a
>>> b
[1, 2, 3]
>>> a
[1, 2, 3]
>>> b.pop()
3
>>> b
[1, 2]
>>> a
[1, 2]
>>> c = copy.copy(a)
>>> c
[1, 2]
>>> a
[1, 2]
>>> c.pop()
2
>>> c
[1]
>>> a
[1, 2]
>>>

b=a creates only references of list (a) addresses.but copy creates entire copy of list(a).


Author: Balaji

Hi..My name is Balaji and i am working as a Senior Software Developer in India. I am interested in Shell scripts, python, erlang , linux kernel .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: