Containers: lists, dictionaries, sets, and tuples

Lists

In Python, a list is the equivalent of an array in C and Java and a vector in matlab; lists are resizable and can contain elements of any type.

Slicing

Lists can also be sliced with the notation [start:end], where start is the start of the slice, inclusive, while end is the end of the slice, exclusive. Mathematically, the interval given by aList[start:end] is the half-open one [start, end). Python is zero-indexed (as opposed to matlab), so the first element in a list is assigned index 0.

Lists can be added to concatenate their elements:

Dicts

Dictionaries, or dicts, store (key, value) pairs and are used as follows:

Sets are used to store distinct elements:

The collections module

The collections module Python has a collections module that contains other useful containers:

Counter

namedtuple

defaultdict