[] and {} vs list() and dict(), which is better?

PythonPerformanceListDictionary

Python Problem Overview


I understand that they are both essentially the same thing, but in terms of style, which is the better (more Pythonic) one to use to create an empty list or dict?

Python Solutions


Solution 1 - Python

In terms of speed, it's no competition for empty lists/dicts:

>>> from timeit import timeit
>>> timeit("[]")
0.040084982867934334
>>> timeit("list()")
0.17704233359267718
>>> timeit("{}")
0.033620194745424214
>>> timeit("dict()")
0.1821558326547077

and for non-empty:

>>> timeit("[1,2,3]")
0.24316302770330367
>>> timeit("list((1,2,3))")
0.44744206316727286
>>> timeit("list(foo)", setup="foo=(1,2,3)")
0.446036018543964
>>> timeit("{'a':1, 'b':2, 'c':3}")
0.20868602015059423
>>> timeit("dict(a=1, b=2, c=3)")
0.47635635255323905
>>> timeit("dict(bar)", setup="bar=[('a', 1), ('b', 2), ('c', 3)]")
0.9028228448029267

Also, using the bracket notation lets you use list and dictionary comprehensions, which may be reason enough.

Solution 2 - Python

In my opinion [] and {} are the most pythonic and readable ways to create empty lists/dicts.

Be wary of set()'s though, for example:

this_set = {5}
some_other_set = {}

Can be confusing. The first creates a set with one element, the second creates an empty dict and not a set.

Solution 3 - Python

The dict literal might be a tiny bit faster as its bytecode is shorter:

In [1]: import dis
In [2]: a = lambda: {}
In [3]: b = lambda: dict()

In [4]: dis.dis(a)
  1           0 BUILD_MAP                0
              3 RETURN_VALUE

In [5]: dis.dis(b)
  1           0 LOAD_GLOBAL              0 (dict)
              3 CALL_FUNCTION            0
              6 RETURN_VALUE

Same applies to the list vs []

Solution 4 - Python

Be careful list() and [] works differently:

>>> def a(p):
...     print(id(p))
... 
>>> for r in range(3):
...     a([])
... 
139969725291904
139969725291904
139969725291904
>>> for r in range(3):
...     a(list())
... 
139969725367296
139969725367552
139969725367616

list() always creates a new object on the heap, but [] can reuse memory cells in many situations.

Solution 5 - Python

IMHO, using list() and dict() makes your Python look like C. Ugh.

Solution 6 - Python

In the case of difference between [] and list(), there is a pitfall that I haven't seen anyone else point out. If you use a dictionary as a member of the list, the two will give entirely different results:

In [1]: foo_dict = {"1":"foo", "2":"bar"}

In [2]: [foo_dict]
Out [2]: [{'1': 'foo', '2': 'bar'}]

In [3]: list(foo_dict)
Out [3]: ['1', '2'] 

Solution 7 - Python

There is no such difference between list() and [] but if you use it with iterators, it gives us:

nums = [1,2,3,4,5,6,7,8]

In: print([iter(nums)])

Out: []

In: print(list(iter(nums)))

Out: [1, 2, 3, 4, 5, 6, 7, 8]

Solution 8 - Python

A difference between list() and [] not mentioned by anyone, is that list() will convert, for example a tuple, into a list. And [] will put said tuple into a list:

a_tuple = (1, 2, 3, 4)
test_list = list(a_tuple) # returns [1, 2, 3, 4]
test_brackets = [a_tuple] # returns [(1, 2, 3, 4)]

Solution 9 - Python

there is one difference in behavior between [] and list() as example below shows. we need to use list() if we want to have the list of numbers returned, otherwise we get a map object! No sure how to explain it though.

sth = [(1,2), (3,4),(5,6)]
sth2 = map(lambda x: x[1], sth) 
print(sth2) # print returns object <map object at 0x000001AB34C1D9B0>

sth2 = [map(lambda x: x[1], sth)]
print(sth2) # print returns object <map object at 0x000001AB34C1D9B0>
type(sth2) # list 
type(sth2[0]) # map

sth2 = list(map(lambda x: x[1], sth))
print(sth2) #[2, 4, 6]
type(sth2) # list
type(sth2[0]) # int

Solution 10 - Python

A box bracket pair denotes one of a list object, or an index subscript, like my_List[x].

A curly brace pair denotes a dictionary object.

a_list = ['on', 'off', 1, 2]

a_dict = { on: 1, off: 2 }

Solution 11 - Python

It's mainly a matter of choice most of the time. It's a matter of preference.

Note however that if you have numeric keys for example, that you can't do:

mydict = dict(1="foo", 2="bar")

You have to do:

mydict = {"1":"foo", "2":"bar"}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNoah McIlraithView Question on Stackoverflow
Solution 1 - PythonGreg HaskinsView Answer on Stackoverflow
Solution 2 - PythonorlpView Answer on Stackoverflow
Solution 3 - PythonThiefMasterView Answer on Stackoverflow
Solution 4 - PythonandreykyzView Answer on Stackoverflow
Solution 5 - PythonRichieHindleView Answer on Stackoverflow
Solution 6 - PythonoxtayView Answer on Stackoverflow
Solution 7 - PythonArunView Answer on Stackoverflow
Solution 8 - PythonKarl WilhelmView Answer on Stackoverflow
Solution 9 - PythonsebtacView Answer on Stackoverflow
Solution 10 - PythonRohit ChaurasiyaView Answer on Stackoverflow
Solution 11 - PythonIkkeView Answer on Stackoverflow