Tuples, Dictionaries, Sets
Tuples have much in common with lists—they are sequences and their elements are accessed with the [] index notation—but they are immutable, which means they must be fully assigned when they are created:
Here are some things you can do to become more familiar with tuples... Practice creating tuples and accessing their values. Make a tuple of tuples and access the subtuples with tuple[x][y] to get the yth element of the xth tuple. Make a tuple of lists. Access an element in one of the lists in the tuple with the same tuple[x][y] method, and try to change it; what happens?
Another common and useful type is the dictionary or dict. Dictionaries are collections of key-value pairs which, unlike list elements, are not ordered in a numerical sequence according to the order in which they were added. Although dictionary values are accessed with [] brackets, the contents of the brackets are not a numerical index indicating position, but rather the key for the desired value. Like lists, dictionaries are mutable: you can add more key-value pairs, remove them from the dictionary, or reassign the value for a given key. The whole collection is enclosed in curly braces, or {}.
In the previous example, my_dict
is a dictionary consisting of two key-value pairs. One key-value pair has the key 'a' with value 1 (later changed to value 'x'). The other has the key 'b' and value 2. A given key cannot appear in a dictionary more than once (since if it did, it would not uniquely map to a value); but a key can map to a container such as a list that holds multiple items. The value can be just about anything, but the key is more limited, in order for the dictionary to be able to look up quickly whether it contains a specified key. Technically speaking, keys in dictionaries must be hashable, where hashing is a process of generating an integer derived from the value of an object. Literals and immutable types like numbers, strings, and tuples are all hashable, and can therefore serve as keys in dictionaries. Lists, on the other hand, are unhashable. You can test whether a proposed key if hashable trying . This function will return an error if the proposed key isn't hashable.
Values can be removed from both lists and dictionaries using the pop()
method. The pop() method returns the removed item.
For practice, you can make your own dictionaries and lists and use pop()
to remove and return specific values.
A set looks like a dictionary without the pairings. It is an unordered collection of unique elements enclosed in curly braces: no duplicates are allowed. The pop() method works on sets as well, but since a set is unordered, the result that is returned is arbritrary. Sets are very useful in large part because they support set algebra, e.g., set unions, intersections, differences, etc.
All of the above types are iterable, and therefore objects of these types can be iterated over in a for-loop such that each element in the object is visited once. As an example, a for-loop can iterate over all the (key, value) pairs in a dictionary (accessed through the dict.items() method); every key-value pair will be visited once, although the particular order in which those pairs are accessed is not guaranteed.
Sets can be iterated over similarly, although as with dictionaries, no particular ordering is guaranteed. Additionally, if you want to iterate over a mutable sequence, like a list, you should be careful not to change the positions of elements as you iterate (unless you really want that behavior!).
The same warning applies to dictionaries and sets, but not to tuples, which are immutable.
*In the code snippet iterating over the key-value pairs in a dictionary, we have used the built-in Python function repr
, which returns the canonical string representation of an object, so that we can print string variables and have them echoed on the screen as strings. Since print
converts its arguments to strings and echoes them to the screen, if we had not used repr
, we would have seen output like key a is associated with value x
.