Why does "[] is [ ]" evaluate to False in python

Try this in an interactive python shell.

[] is [ ]

The above returns False, why?

You created two mutable objects, then used is to see if those are the same object. That should definitely return False, or something would be broken.

You wouldn't ever want is to return true here. Imagine if you did this:

foo = []
bar = []

then you'd be very surprised if bar now contains 42. If is returned true, meaning that both [] invocations returned the exact same object, then appending to foo would be visible in the reference to bar.

For immutable objects, it makes sense to cache objects, at which point is may return true, like with empty tuples:

>>> () is ()  # are these two things the same object?

The CPython implementation has optimised empty tuple creation; you'll always get the exact same object, because that saves memory and makes certain operations faster. Because tuples are immutable, this is entirely safe.

If you expected to test for value equality instead, then you got the wrong operator. Use the == operator instead:

>>> [] == []  # do these two objects have the same value?

Why does coronavirus kill some people and spare others?, Scientists are exploring genetic factors, including a person's blood type, how the virus is transmitted, and whether particular strains might be� Does it take you forever to make a doctor’s appointment, clean out your garage, or do your taxes? Putting off something that needs to be done is called procrastination.We all do it sometimes.

In python is does a reference equality check like [] and [] they are different objects you can check that by

print id([]),id([])


 In [1]: id([])
Out[1]: 140464629086976

In [2]: id([])
Out[2]: 140464628521656

both will return different address and both are different object so is will always give false

[] is []



Why do some COVID-19 patients infect many others, whereas most , Is it true that getting vaccinated repeatedly can reduce vaccine effectiveness? Why are there so many different outcomes for vaccine effectiveness studies? How� Why Does Trump Lie? It’s mortifying enough to imagine the damage he can do in the next five months, let alone the following four years if he’s re-elected.

[] is like list(), if you do this:

a = list()
b = list()

clearly a and b are two completly different objects, hence:

a is b # False


list() is list() # False


[] is [] # False

Vaccine Effectiveness: How Well Do the Flu Vaccines Work?, Why Do Clocks run Clockwise? asks the title of a book by David Feldman. Why indeed! This odd book looks at 239 such questions. First they look innocent� You may have noticed that after eating asparagus, your pee has a somewhat unpleasant scent. This article explains why eating asparagus makes pee smell and why only some people can smell it.

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.

id('') : 139634828889200
id('') : 139634828889200
id('') : 139634828889200

id([]) : 139634689473416
id([]) : 139634689054536
id([]) : 139634742570824

No. 424: Why Does? Why Is?, Why does Sargassum smell so bad? When washed ashore, Sargassum will decompose (rot). Rotting Sargassum causes the production of hydrogen sulfide gas� But why does it rotate at all? The Earth formed out of a disk of gas and dust that swirled around the newborn sun. In this spinning disk, bits of dust and rock stuck together to form the Earth,

[PDF] Sargassum, But health experts say the evidence is clear that masks can help prevent the Why did the CDC change its guidance on wearing masks? Caffeine is the most widely consumed stimulant in the world.That being said, it does not affect everyone in the same way. Some people, for example, can drink multiple cups during the day and

Still Confused About Masks? Here's the Science Behind How Face , An Xiao Mina joins us to discuss why the new kid on the social media block is causing waves in halls of power around the world. Purrs release feel-good endorphins, so experts think cats use the vibrations to soothe themselves. That could mean your cat purring while enjoying some cozy cuddles from you, or it might help calm

Why Does TikTok Bother the Powerful So Much?, Researchers have identified microscopic features that could make the pathogen more infectious than the SARS virus — and serve as drug� The carriers for CDMA do not have to accept your phone onto its network. What Is a SIM Card and Why Do You Need One? If you have never seen a SIM card, it basically looks like a tiny piece of plastic.

  • Maybe you should explain why you think it should be true?
  • [] is [] will also fail: the two are different objects. is does a reference equality check.
  • Why should this be true? You created two empty mutable objects, you wouldn't want them to be the same object.
  • @Giacomod'Antonio: that's easy to research however. The OP didn't share any of their research, nor did they explain why they expected this expression to be true. You are assuming that they confused is with == (the value equality operator), but perhaps they thought it would be efficient instead? For example, for two empty tuple expressions this would be true: () is (), as is 1 is 1.
  • @BillalBEGUERADJ: badly researched != off-topic. And I'm assuming there was no research only because nothing in the question shows otherwise.
  • Those id tests aren't doing what you think they are. It's quite common for the print to print the same number twice, because the lists don't have overlapping lifetimes. Similarly, this is why your == test with id values gave True instead of False.
  • Why does it matter what way the empty list objects were created?
  • @MartijnPieters, it does not matter, maybe i did not explained well, or you did not understand what i would like to explain?
  • What I mean is that it doesn't matter if you use [] is [] or list() is list(); both expressions create empty list objects. To say that [] is like list() you are not explaining anything about why the is test returns False, because the difference between [] and list() doesn't matter here. Also, your == False tests are incorrect; you are testing if (a is b) and (b == False), because Python comparison operators are chained.
  • @MartijnPieters, ok so i explained badly, i was just poiting that [] and list() both build objects, and in each call both build diferent objects, so it was obvious that [] is [] would be evaluated to false. About the operators, it was just me relaxing and writting python sintax for making the point, not written reliable python code, sorry.