educative.io

Why does"print(list1 is list2)" not return True?

list1 = [6,7,8]
list2 = [6,7,8]
print(list1 is list2)

This returns False acccording to your terminal. I don’t understand why. Shouldn’t it return True? If all the elements of two lists are the same, are they not the same lists? Thank you for any explanation.

2 Likes

Hi @Hajnalka_Toth,

Yes, all elements in the two lists are the same plus in the same order, but why does it return False?. Well, you might have noticed that there’s a is keyword in the print() statement. And what that does is “matches the references (not the values/elements) of those two lists”!

list1 and list2 are created on different memory locations, which is why the print(list1 is list2) returns False, which should be the expected output.

Note: If you change list1 is list2 to list1 == list2, then you would get a True value because the “==” operator compares the values of these two lists.

Thank you so much, and hope this helps.

1 Like

Thank you so much for the quick response. Yes, that definitely helps, I guess I just wasn’t clear on what “comparing the references” meant! I understand now, thank you again!

1 Like