2

I have a code like below,when i print the list1 and list2 it shows same elements but i have added the 9 after the assignment of existing list1 to list2 so it should not show 9 in list2.

list1=[1,2,3,4]
list2=list1
list1.insert(4,9)
print(list1)
print(list2)

please clear my doubt.

hieko
  • 383
  • 1
  • 5
  • 13
  • 3
    Unlike other languages, in Python, `list2=list1` does _not_ make a separate copy of list1. list1 and list2 refer to the _same_ value. See https://nedbatchelder.com/text/names.html for a more detailed explanation. – John Gordon Dec 27 '17 at 04:16
  • 6
    Possible duplicate of [python list by value not by reference](https://stackoverflow.com/questions/8744113/python-list-by-value-not-by-reference) – Ajax1234 Dec 27 '17 at 04:23
  • i understand the point. thanks for your help. – hieko Dec 27 '17 at 04:30
  • 1
    Please search the web before asking questions , this question has about 80k views and so it can be easily reached! – Ubdus Samad Dec 27 '17 at 11:38

3 Answers3

8

In python, a variable name is a reference to the underlying variable. Both list1 and list2 refer to the same list, so when you insert 9 into that list, you see the change in both. You need to make an explicit copy (using the copy module, slice notation list2 = list1[:], or some other method) if you want them to be distinct.

Daniel H
  • 7,223
  • 2
  • 26
  • 41
  • what if i need that kind of requirement, i want a make a copy of list in another list for some purpose. how would i do that. please comment. – hieko Dec 27 '17 at 11:41
  • @hieko, look at [Amit's answer](https://stackoverflow.com/a/47992113/14062356). you should use either `list2 = list1[:]` or `list2 list1.copy()`. I'd recommend the second since it is clearer. – Ac Hybl Dec 05 '21 at 15:32
4

You are confused between,

when we have different lists? and when an alias is created?.

As you have written:

list1=[1,2,3,4]
list2=list1

The above code snippet will map list1 to list2.

To check whether two variables refer to the same object, you can use is operator.

>>> list1 is list2
# will return "True"

In your example, Python created one list, reference by list1 & list2. So there are two references to the same object. We can say that object [1,2,3,4] is aliased as it has more than one name, and since lists are mutable. So changes made using list1 will affect list2.

However, if you want to have different lists, you should do this:

>>> list1 = [1, 2, 3, 4]
>>> list2 = list1[:]  # here list2 is created as a copy of list1
>>> list1.insert(4, 9)
>>> print list1
[1, 2, 3, 4, 9]
>>> print list2
[1, 2, 3, 4]
Amit Upadhyay
  • 7,179
  • 4
  • 43
  • 57
0

list1=[1,2,3,4] then list1= list2 which means the id of both lists is the same. even the values are the same then list1.insert(4,9) it means to insert the value 9 at index position 4 then the list1 is [1,2,3,4,9] we know the id of both list1 and list2 is the same then the list1 and list2 values are also the same list1=[1,2,3,4,9] list2=[1,2,3,4,9]

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 17 '22 at 22:02