What is the difference between [None] and [] in python?

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

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

When should I use None? and []

and another interesting question

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

why is the empty list id id different?

+4
source share
5 answers

[] - empty list

[None]is a list with one item. This item isNone

isverification of reference equality. If both objects belong to the same object by reference, then isreturns true.

a = []
b = a
a is [] #false
a is b  #true
+9
source

[None]doesn’t mean that there is nothing on the list. Noneis a python keyword that has special meaning. This is similar to NILor NULLin other languages.

[None], : " , None". " , " ( []).

+1

1:

- . "NoneType".
, - :

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

, , .

2:

Python, = . , , . , a b. , , is, , .

, ( []), .

+1

None , . , None.

() is. ==!

is , , . :

>>> 1900 is 1900
True

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

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

, , : Python '1 is 1 ** 2' , '1000, 10 ** 3'?

, ==:

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

.

+1

None, . [] , .

[None] - , None

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

In Python, x is yis used to check whether the tags xand ythe same. Here cthey dpoint to different objects of the list. so,

>>>print c is d
False

.

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
0
source

All Articles