A Few Notes on Python’s Built-In Container Types

I’ve been learning Python recently, as I’m planning to use it for some future projects. One of the things I’ve initially found a bit confusing is the different types of built-in containers that Python provides: Lists, Tuples, Sets and Dictionaries. So this post is just a few notes for myself on how these work and what they’re useful for. There’s nothing here that you can’t get from the excellent documentation at https://docs.python.org.jar

Lists

These are like arrays in most other languages: a mutable sequence of values. Lists can be created using square brackets:
my_list = []
my_list = [1]
my_list = [1, 2, 3, 4]

Or using the list constructor:
my_list = list()
my_list = list(iterable)
(Where iterable is any iterable type. See https://docs.python.org/3.4/library/stdtypes.html#iterator-types)

Or using a list comprehension:
my_list = [x for x in iterable]

When using the Python console, lists get displayed in square brackets.

Lists are sequences, so they support a bunch of operations that sequences support. These include slicing, in, not in, concatenation with +, making multiple copies with *, len, min, max, index and count. See https://docs.python.org/3.4/library/stdtypes.html#common-sequence-operations

Lists are mutable sequences, so they support some other operations that manipulate the contents of the list. These include replacing slices, del, append, clear, copy, extend, insert, pop, remove, reverse. See https://docs.python.org/3.4/library/stdtypes.html#mutable-sequence-types.

Lists also have an operation called sort. See https://docs.python.org/3.4/library/stdtypes.html#list.sort.

Tuples

Tuples are basically like lists, but they are immutable. Tuples can be created using commas:
my_tuple = 1, # Note the trailing comma here.
my_tuple = 1, 2, 3, 4

Sometimes you need parentheses to resolve ambiguous code, so these are also supported (this also allows you to create an empty tuple):
my_tuple = ()
my_tuple = (1,) # need the trailing comma, otherwise you just get "my_tuple = 1"

my_tuple = (1, 2, 3)

Or you can use the tuple constructor:
my_tuple = tuple()
my_tuple = tuple(iterable)

When using the Python console, tuples get displayed in parentheses.

As sequences, tuples support all the standard sequence operations (in, not in, slicing etc). As tuples are immutable, they don’t support any of the mutable operations that lists support (append, pop etc).

Sets

Sets are unordered, and every value is unique. Sets can be created using curly braces:
my_set = {}
my_set = {1, 2, 3}

Or using the set() constructor:
my_set = set()
my_set = set(iterable)

If you create a set that contains duplicate values (e.g. my_set = {1, 1, 2, 2, 3, 4}), the duplicates will be discarded and the set will only contain a single item of each value (i.e. the result will be {1, 2, 3, 4}). The same applies when adding new items; if the item is already in the set, nothing will be added.

When using the Python console, sets get displayed in curly braces.

Sets support lots of useful operations for comparing sets and checking the membership of items. These include in, not in, disjoint, issubset, issuperset, union, intersection, difference and symmetric_difference. There are also some operator overloads for some of these. See https://docs.python.org/3.4/library/stdtypes.html#set-types-set-frozenset.

There is an immutable version of set, called frozen_set. This can only be created with the frozen_set constructor, and it doesn’t support any of the operations that modify a set.

Dictionaries

A dictionary is a mapping between keys and values. Every key is unique. Dictionaries can be created with curly braces, separating keys from values with a colon:
my_dict = {"a": 1234, "b": "Fred Smith", "c": -9999}

You can’t make an empty dictionary with curly braces (you’d get an empty set instead), but you can with the dict constructor.
my_dict = dict()
my_dict = dict({"a": 1234, "b": "Fred Smith", "c": -9999})

You can also use the dict constructor by supplying a list (or another iterable type) containing pairs of items for the keys and values:
my_dict = dict([("a", 1234), ("b", "Fred Smith"), ("c", -9999)])

If you create a dictionary that contains duplicate keys (e.g. my_dict = dict({"a": 123, "b": 456, "a": 789})), the duplicates will be discarded and the set will only contain a single item for each key. Python seems to take the last value if there’s a duplicate (i.e. the result will be {"a": 789, "b": 456}).

When using the Python console, dictionaries get displayed in curly braces. Note that an empty dictionary looks the same as an empty set; both are displayed as {}.

Dictionaries support several useful operations, including in, not in, iter (over keys), clear, copy, views on values, keys and items. See https://docs.python.org/3.4/library/stdtypes.html#mapping-types-dict.