Lists, Tuples and Dictionaries

Python ships with powerful data structures. The most important are list, tuple, set and dictionary.

List

Mutable, ordered, arbitrary elements: numbers = [1, 2, 3]. Methods: append(), extend(), sort(), pop().

Tuple

Immutable, fast: point = (10, 20). Great for coordinates or fixed records. Unpacking: x, y = point.

Set

Unique elements: tags = {"python", "web"}. Operations: | (union), & (intersection), - (difference).

Dictionary

Key-value pairs: user = {"name": "Anna", "age": 32}. Access user["name"], safer user.get("name"). Iterate: for k, v in user.items():.

Comprehensions

squares = [n*n for n in range(10)]
evens = {n for n in range(20) if n % 2 == 0}

See also: Python for Beginners.