What happens if you compare lists in python? #python #learnpython #programming

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

It’s because “is” checks whether two variables have the same memory location, and lists always occupy a new memory location when initialised.

kingoreo
Автор

I believe it's that lists are mutable, so a new list is generated each time when list() is called, meaning that the 2 lists in the comparison use different memory location

However, tuples are not mutable, so python automatically shares their location when it finds that both tuples are the same. This happens to other immutable things like numbers and strings as well

Tell me if I'm wrong

not_vinkami
Автор

When we use the "is" operator, essentially it compares the memory address in the RAM.
Since tuple is an immutable object hemce it points to the same memory address, while list due to its mutability allocates two different addresses even if they have exactly same content.

Therefore list() is list() evaluates to False

GodOfGodsOfTheKnownReality
Автор

Something to do with mutability property for both classes?? I am newB

adilshahid