12

I think [None] is same as [], but in my test , maybe there is something...

>>>print len([])
0
>>>print len([None])
1

when should i use the None ? and []

and another interesting question

>>>c= []
>>>d= []
>>>print c is d
False
>>>a= 1
>>>b=1
print a is b
True

why empty list's id granting that is different?

minssi
  • 333
  • 1
  • 2
  • 10
  • 3
    1. `[]` - a list wihtout any item in it. `[None]` - a list with a `None` object in it. – falsetru Apr 29 '16 at 02:36
  • 2. You need to use `==` instead of `is` to check equality. Use `is` to check identity (for singleton objects like `None`, `True`, `False`) – falsetru Apr 29 '16 at 02:36
  • @falsetru: those don't ask for more information or suggest improvements, and so shouldn't be comments. – DSM Apr 29 '16 at 02:38
  • 1
    `is` checks if two variables point to same object whereas `==` checks if the objects referred to by the variables are equal. See http://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is-in-python – Ozgur Vatansever Apr 29 '16 at 02:38
  • http://stackoverflow.com/questions/13805882/empty-list-is-equal-to-none-or-not – Chen Xu Apr 29 '16 at 02:38
  • I don't think it is fair to down vote such a question... +1 to even. – Julien Apr 29 '16 at 02:40
  • Whether to use 'None' or an empty list would depend on you application. Is there something specific you are trying to do? – EngineerCamp Apr 29 '16 at 02:40
  • Next time, please do basic research via Google. Queries like "python what is none" and "python list is list" will return very useful results. In fact, the first result for the former is the linked duplicate, and the first result for the latter is [this](http://stackoverflow.com/questions/3718513/python-is-statement-what-is-happening), which is a perfect explanation for your identity comparison of lists. – TigerhawkT3 Apr 29 '16 at 03:44
  • Note `a = [None] * 5` will create a list of length 5 with all elements being `None`. Conversely, if you do `b = [] * 5`, the list will still be `[]`. I have seen [people](https://github.com/morganb-phys/VWaves-GaiaDR2/blob/master/py/NumberCount.ipynb) using `[None]` to initialise a list of a certain length and then assign values to the entries. – Luismi98 Nov 04 '20 at 14:51

5 Answers5

12

[] is an empty list

[None] is a list with one element. That one element is None

is checks for reference equality. If both objects refer to the same object by reference then is will return true.

a = []
b = a
a is [] #false
a is b  #true
johan
  • 1,664
  • 2
  • 14
  • 30
gnicholas
  • 2,041
  • 1
  • 21
  • 32
3

[None] does not mean that there is nothing in the list. None is a keyword in python which has a special meaning. It is like NIL or NULL in other languages.

When you say [None], you are saying "I would like to have a list which contains the special object called None". This is different than saying "I would like a list which contains no elements" (by typing []).

Frank Bryce
  • 8,076
  • 4
  • 38
  • 56
2

Question 1:

None is an object. It is of type "NoneType".
This can be seen by doing something like this in the terminal:

>>> type(None)
<type 'NoneType'>

So, when you put this object in a list, the list has one element.

Question 2:

The assignment operator in Python, =, is used to attach a name to an object. In the case of immutable objects, like integers, multiple names can be attached to the same object. That is what you are doing with a and b. So, when you test their identity, using the is operator, you see that the two names point to the identical object.

Alternatively, when you attach a name to a newly created list (which you created with the [] operator) it is a different list each time.

John Mulder
  • 9,765
  • 7
  • 33
  • 37
2

None is a valid element, but you can treat it like a stub or placeholder. So it counts as an element inside the list even if there is only a None.

For (equality) comparisons you shouldn't use is. Use ==!

Because is can lead to strange behaviour if you don't know exactly when and how to use it. For example:

>>> 1900 is 1900
True

>>> a = 1900
>>> b = 1900
>>> a is b
False

>>> a, b = 1900, 1900
>>> a is b
True

This rather strange behaviour is explained for example in this question: Why does Python handle '1 is 1**2' differently from '1000 is 10**3'?

This won't happen when you use ==:

>>> a == b
True
>>> 1900 == 1900
True

like one would expect.

Community
  • 1
  • 1
MSeifert
  • 145,886
  • 38
  • 333
  • 352
0

You want to use None to imply that there is no valid object. You want to use [] to imply an object that is of type list and has no elements.

[None] is a list with one element which is None

>>>c= []  # This is a new list object
>>>d= []  # This is another new list object

In Python , x is y is used to check whether x and y are the same objects. Here, c and d point to different list objects. so,

>>>print c is d
False

is expected.

On the other hand,

>>>c= []  # This is a new list object
>>>d = c  # This is the same object as c
>>>print c is d
True

Here, a and b are primitives, not objects

>>>a= 1
>>>b=1

So, this is expected:

print a is b
True
trans1st0r
  • 2,023
  • 2
  • 17
  • 23
  • 1
    There's actually a trick here for using is on primitives in Python. As everything in Python is an object, even ints are. The reason why 1 is 1 will return true is because Python caches small integer objects for performance reasons. Try 10000000 is 10000000 and it will return false. – gnicholas Apr 30 '16 at 16:06