Why it's True False False? | Sololearn: Learn to code for FREE!


Why it's True False False?

Plaese take a look at my Python code. Can you explain me the 'is' operator in this case? https://code.sololearn.com/c88nbF0EdLW2/?ref=app

6/2/2020 7:00:14 AM

Kiuziu 💘 Berlin

8 Answers

New Answer


list1 = [1,2] >create a list and store it in list1 list2 = list1 >assign list1 to list2..meaning list2 is also pointing to same object(i think python call value as object)..so there are both pointing to same thing list3 = list2[:] >that[:] in list will return a new list..it will have same value as list2 but it is a new one.. "is" keyword will only return true if u compare two variable that point to same object


The id is different, it is another object, try this adding to your code. print (id(list1)) print (id(list2)) print (id(list3))


Okay Lily Mea final test: L1 = [[1,1],[1,2]] L2 =L1[:] L1[0][0]=3 What is L2 now and why?


Additionally to HonFu s great tutorial I would propose that you read all functions for a list object (Sort,reverse,....) and find out if the result creates a new reference or works on list itself(inplace).


Manuel Prochnow Bcoz list 1 & list 2 have the same memory address You can check it by using id function print(id(list1)) print(id(list2)) Output : 2903389768 2903389768 But list3 has different memory address print(id(list3)) Output :. 2901078464


After reading this, all of that sort of questions should become clear... I hope. https://code.sololearn.com/c89ejW97QsTN/?ref=app


Lily Mea Now I understand the 'is' operator. It compares the ID of the variable and not its value. Thanks for explanation to all answers


Oma Falk [[3,1],[1,2]] & L1 = [[1,1],[1,2]] L2 =L1 L1[0][0]=3 Same