← CS Lab Programming Foundations / Python Data Structures Open standalone ↗

Python ships four built-in containers, and the one you pick decides what your data can and can't do. list, tuple, set, and dict can hold the same values — but they differ on order, duplicates, mutability, and lookup speed.

Where you meet it

What it does

Each container enforces a different contract. A list is ordered and editable. A tuple is the same but frozen. A set drops order and duplicates. A dict maps keys to values. Choosing the right one is the difference between clean code and a workaround.

Picking the container is a design decision, not a detail: order, duplicates, and O(1) lookup are baked in before you store a single value.

How it works

list — ordered, mutable, allows duplicates. Use it when position and editing matter:

nums = [3, 1, 4, 1, 5]
nums.append(9)     # [3, 1, 4, 1, 5, 9]  — order kept, 1 stays twice

tuple — ordered but immutable. Use it for fixed groupings; it can't be changed after creation:

point = (3, 4)
point[0] = 9       # TypeError — tuples are frozen

set — unordered, no duplicates. Use it for uniqueness and fast membership:

seen = {3, 1, 4, 1, 5}   # {1, 3, 4, 5} — the second 1 is dropped
4 in seen                # True — O(1) check

dict — a key→value hashmap with O(1) average lookup. Use it to fetch by name, not by position:

ages = {'a': 3, 'b': 1}
ages['a']          # 3 — instant, no scanning

Sets and dicts are backed by a hash table: that's why x in s and d[key] stay fast as the data grows, while scanning a list for a value is O(n).

Watch out

  • Set elements and dict keys must be hashable — usually immutable. A list can't be a dict key or a set member; a tuple can.
  • Set iteration order isn't guaranteed. A set has no positions — don't rely on the order elements come out. (A dict does keep insertion order since 3.7, but a set does not.)
  • Never use a mutable default argument like def f(items=[]). The list is created once and shared across calls — use None and build it inside.

Go deeper

Python bringt vier eingebaute Container mit, und die Wahl entscheidet, was deine Daten können und was nicht. list, tuple, set und dict können dieselben Werte halten — unterscheiden sich aber in Reihenfolge, Duplikaten, Veränderbarkeit und Lookup-Geschwindigkeit.

Wo es vorkommt

Was es tut

Jeder Container erzwingt einen anderen Vertrag. Eine list ist geordnet und änderbar. Ein tuple ist dasselbe, aber eingefroren. Ein set wirft Reihenfolge und Duplikate weg. Ein dict bildet Schlüssel auf Werte ab. Den richtigen zu wählen ist der Unterschied zwischen sauberem Code und einer Krücke.

Die Container-Wahl ist eine Design-Entscheidung, kein Detail: Reihenfolge, Duplikate und O(1)-Lookup stehen fest, bevor du einen einzigen Wert ablegst.

Wie es funktioniert

list — geordnet, veränderbar, erlaubt Duplikate. Nimm sie, wenn Position und Bearbeiten zählen:

nums = [3, 1, 4, 1, 5]
nums.append(9)     # [3, 1, 4, 1, 5, 9]  — Reihenfolge bleibt, 1 doppelt

tuple — geordnet, aber unveränderlich. Nimm es für feste Gruppierungen; nach dem Erstellen nicht mehr änderbar:

point = (3, 4)
point[0] = 9       # TypeError — Tupel sind eingefroren

set — ungeordnet, keine Duplikate. Nimm es für Einzigartigkeit und schnelle Mitgliedschaft:

seen = {3, 1, 4, 1, 5}   # {1, 3, 4, 5} — die zweite 1 fällt weg
4 in seen                # True — O(1)-Prüfung

dict — eine Key→Value-Hashmap mit O(1)-Lookup im Schnitt. Nimm es, um per Name zu holen, nicht per Position:

ages = {'a': 3, 'b': 1}
ages['a']          # 3 — sofort, ohne Durchsuchen

Sets und Dicts liegen in einer Hash-Tabelle: darum bleiben x in s und d[key] schnell, auch wenn die Daten wachsen — während das Durchsuchen einer Liste nach einem Wert O(n) ist.

Worauf achten

  • Set-Elemente und Dict-Keys müssen hashbar sein — meist also immutable. Eine list kann kein Dict-Key und kein Set-Element sein; ein tuple schon.
  • Die Iterations-Reihenfolge eines Sets ist nicht garantiert. Ein Set hat keine Positionen — verlass dich nicht darauf, in welcher Reihenfolge Elemente herauskommen. (Ein dict behält seit 3.7 die Einfüge-Reihenfolge, ein Set nicht.)
  • Nie ein veränderliches Default-Argument wie def f(items=[]) nutzen. Die Liste wird einmal erzeugt und über alle Aufrufe geteilt — nimm None und bau sie drinnen.

Mehr dazu

Next up
Programming FoundationsPython Mutability & References Data Science & Statisticspandas .loc vs .iloc All concepts →