Just wondering: what is the difference in between list = list()
and list = []
, and in between dict()
and {}
?
If none, which is the most Pythonic?
Thanks for your help!
Asked
Active
Viewed 35 times
0

bdooyr
- 1
- 1
-
Using the literals in IMHO, is more *pythonic*. – Dani Mesejo Mar 25 '20 at 14:59
-
There's no semantic difference. Literals are more pythonic. – Jared Smith Mar 25 '20 at 14:59
-
1[What's the difference between list() and `[]`](https://stackoverflow.com/q/33716401/1324033) – Sayse Mar 25 '20 at 15:00
-
A lot of info out there already. For example, [How to create an empty list in python](https://java2blog.com/create-empty-list-python/) – lurker Mar 25 '20 at 15:01
-
1The shorter [] and {} is more Pythonic. There is no real difference (as long as the names "list" or "dict" aren't overwritten by something) although the short variants (literals) are slightly faster. – Michael Butscher Mar 25 '20 at 15:01
-
The literals are more efficient as well; the compiler can allocate the object immediately, before the code runs, rather than waiting until runtime to call whatever the name is bound to. (A compiler will typically not undertake the analysis needed to determine if the name has been rebound.) – chepner Mar 25 '20 at 15:02
-
Reiterating [Alex Martelli's answer](https://stackoverflow.com/a/2745292/5431791) : `dict()` is more "pronounceable", whereas `{}` is faster. Rest depends on opinion, according to Alex Martelli `dict()` is better as it is more explicit. – Sayandip Dutta Mar 25 '20 at 15:03
-
Is nobody going to mention that there IS a difference? 'list()' can be used to consume the contents of an iterator or generator, whereas the literal '[]' would just create a list with an iterator reference in the same context. – Paul M. Mar 25 '20 at 15:23