+30

Why does it print false?

lap_list = [] nap_list = [] print(nap_list is lap_list)

4/18/2019 9:12:42 AM

Soheil Elahi

17 Answers

New Answer

+23

"is" checks the id of each list-object and not the elements. It behaves similar to print(id(nap_list) == id(lap_list))

+14

As Tom Packard and Anna said, nap_list and lap_list have not the same id, so if you want to compare them, it will be false.

+13

lap_list is an empty list. nap_list is an empty list. They have the same value (lap_list == nap_list), but they are not the same list (nap_list is not lap_list).

+13

Another question, how could I give them same ids?

+11

"Is" function is use to compare the id of each object. The == operator compares the values of both the operands and checks for value equality. Whereas( is) operator checks whether both the operands refer to the same object or not. This. Code can be written as print(id(lap_list)==id(nap_list)) Thanks

+9

Alhussein Gomma == operator

+7

Lito Delcid what if i want to compare the elements inside?? what should i use??

+6

Use == operator to compares the two values.

+6

The IDs of two lists are different. This would be True:👇 lap_list=[] nap_list=lap_list print(nap_list is lap_list)

+5

Because in this both list has different id that why it is false

+3

Moon and back to sleep now

+3

to do it properly you could use == operator

+2

because their memory space is different. U can check it with id(variable)

+1

vitalii i think u should wrap list comprehension part inside [ ] edit : and u are raising error yourself in case of "pineapple"

+1

As Tom Packard and Anna said, nap_list and lap_list have not the same id, so if you want to compare them, it will be false.!!!!!!!!

+1

All is id

0

Why is appearing a mistake in the next code? class Pizza: def __init__(self, toppings): self.toppings = toppings @staticmethod def validate_topping(topping): if topping == "pineapple": raise ValueError("No pineapples!") else: return True ingredients = ["cheese", "onions", "spam", "pineapple"] if all(Pizza.validate_topping(i) for i in ingredients): pizza = Pizza(ingredients) print (ingredients)