I know about foo = []
being shorthand for foo = list()
and I was wondering if there was a list of shorthand notations for creating empty dictionaries, tuples, sets, etc. I'm specifically looking for Python 3.x but either one would be useful to have.
Asked
Active
Viewed 635 times
-1

Nakaan
- 167
- 7
-
1`[]` is not shorthand. It is 'literal notation', and faster. See [Why is \[\] faster than list()?](http://stackoverflow.com/q/30216000) – Martijn Pieters Feb 23 '16 at 16:38
-
But please go to the [Python tutorial](https://docs.python.org/3/tutorial/) for basic questions like these. It covers all the basics, including [literal notations for data structures](https://docs.python.org/3/tutorial/datastructures.html). – Martijn Pieters Feb 23 '16 at 16:39
-
Thanks for pointing me to that question, I'll have to give that a read. I also didn't know it was considered a literal (though I probably should have realized) I must be blind if I didn't see the literals in the docs. – Nakaan Feb 23 '16 at 16:41
-
1Because the notation for containers is more complex than for strings, integers, etc, you find those defined as *displays*, in the [*Expressions* section](https://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries). – Martijn Pieters Feb 23 '16 at 16:43
1 Answers
1
To create an empty dictionary, use {}
.
To create an empty tuple, use ()
.
There is no shortcut for an empty set.
As for the etc., I don't know what else you are thinking of.

zondo
- 19,901
- 8
- 44
- 83