Dictionaries or sets are created in braces. Square brackets create lists .
They are called literals; set of literals:
aset = {'foo', 'bar'}
or dictionary literal:
adict = {'foo': 42, 'bar': 81} empty_dict = {}
or list literal:
alist = ['foo', 'bar', 'bar'] empty_list = []
To create an empty set, you can only use set() .
Sets are collections of unique items and you cannot order them. Lists are ordered sequences of items, and values ββcan be repeated. Dictionaries map keys to values; keys must be unique. Dial and dictionary keys must also meet other restrictions, so Python can effectively track them and know that they are and will remain unique.
There is also a tuple type, using a comma for 1 or more elements, with parentheses being optional in many contexts:
atuple = ('foo', 'bar') another_tuple = 'spam', empty_tuple = () WARNING_not_a_tuple = ('eggs')
Note the comma in the definition of another_tuple ; it is a comma that makes it tuple , not a bracket. WARNING_not_a_tuple not a tuple, it does not have a comma. Without parentheses, all you have left is a string.
See the Python textbook data structure chapter for more details; lists are presented in the chapter.
The literals for such containers are also called displays , and the syntax allows you to program the creation of content based on loops called understandings.
Martijn Pieters Mar 13 '14 at 21:35 2014-03-13 21:35
source share