Python List vs Tuple vs Set vs Dictionary

Compare Python list, tuple, set, and dictionary with examples. Learn the differences in order, mutability, duplicates, indexing, syntax, use cases, and when to use each data type.

Published

Updated

Read time 8 min read

Reviewed byDeepak Prasad

Python List vs Tuple vs Set vs Dictionary

You choose between a list, tuple, set, and dict based on whether you need order, duplicates, keys, and in-place changes. When you care about how many items a container holds, len() applies to all four. The Python tutorial on data structures describes lists and tuples as sequence types, sets as unordered collections of unique elements, and dictionaries as mappings from keys to values. This page gives a fast mental model, then pairwise comparisons, tables, and runnable examples so you can pick a type with confidence.

Tested on: Python 3.13.3; kernel 6.14.0-37-generic.


Quick difference between list, tuple, set, and dictionary

List Tuple Set Dictionary
Order Yes, insertion order Yes, insertion order Unordered in the abstract; iteration order is implementation-defined Insertion order preserved (3.7+); keyed, not index-by-position for keys
Duplicates Allowed Allowed Not allowed (unique elements) Keys must be unique; values can repeat
Lookup style Integer index seq[i] Integer index seq[i] Membership x in s (no indexing) Key lookup d[k]
Typical mutability Mutable Immutable Mutable (frozenset is the immutable variant) Mutable (keys must stay hashable)

If you only remember one line: list and tuple give ordered positions, a set is a unique bag, and a dict is labeled fields. To walk positions while you iterate, pair a list (or tuple) with enumerate() or a classic for loop.


What list, tuple, set, and dictionary are

List

A list is a mutable ordered sequence. You append, insert, pop, and sort in place; mixed types are fine. Use a list when you need stable order, duplicates, integer indexing, or stack- or queue-style edits at the ends (JSON arrays map naturally to lists). For list pop by index and list extend from another iterable, see those guides. Building from logic or other sequences often involves list comprehension or concatenating lists.

python
items = [1, "two", 3.0]
items.append(4)
Output

After append, items has four elements in order.

Tuple

A tuple is an immutable ordered sequence: you do not resize the same object in place; you build a new tuple when contents change. Tuples fit fixed records (coordinates, DB rows) and dict keys when every element is immutable. Prefer a tuple when shape is fixed and you want read-only semantics for callers; prefer a list when you keep editing the same collection.

python
point = (10, 20)
single = (42,)  # trailing comma matters for a one-tuple
Output

Set

A set stores hashable unique elements with fast membership tests. There is no s[i] indexing—only iteration and in. The mutable type is set; frozenset is immutable. For combining collections, see set intersection and set difference. Use a set when uniqueness and membership matter more than order; for ordered unique passes, dict.fromkeys(...) or explicit list logic works.

python
tags = {"read", "write", "read"}  # duplicates collapse
tags.add("admin")
Output

Dictionary

A dictionary maps hashable unique keys to values; lookups are by key, not by sequential index. From Python 3.7 onward, dict preserves insertion order when you iterate. Use a dict for named fields, sparse or non-integer keys, or O(1) average-time key lookup (JSON objects map naturally). For updates see add to dictionary and nested dictionaries. To sort key views, use sort dictionary by key or by value.

python
user = {"name": "Ada", "role": "dev"}
user["role"] = "admin"
Output

List vs tuple in Python

Lists grow and change in place; tuples do not, which is why lists suit buffers and tuples suit “frozen” rows and hashable keys. If two variables share one list, both see edits—copy patterns for lists help when you need isolation. Tuples are often a bit lighter than lists for the same length because the implementation does not reserve growth slack; the clarity of mutable vs immutable usually matters more than a micro speed win. Choose a list when size or elements change often; choose a tuple for fixed-shape records, hashable keys, or APIs that promise callers the sequence stays unchanged.

python
a = [1, 2]
a[0] = 99

b = (1, 2)
try:
    b[0] = 99
except TypeError as e:
    print("tuple:", type(e).__name__)
Output

You should see tuple: TypeError because tuple items cannot be reassigned.


Tuple vs set in Python

Tuples keep order and allow repeats; sets enforce uniqueness and never give you a stable “ith element” index—tuple[i] works, but set[i] raises TypeError because sets are not sequences. Use a tuple when you need ordered data with possible duplicates; use a set when uniqueness and membership dominate and you do not need positional indexing.

python
t = (1, 2, 1)
s = {1, 2, 1}
print("tuple first:", t[0], "len tuple:", len(t), "len set:", len(s))
try:
    print(s[0])
except TypeError as e:
    print("set indexing:", type(e).__name__)
Output

You should see tuple first: 1 len tuple: 3 len set: 2 (the tuple keeps both 1s; the set stores one), then set indexing: TypeError because sets are not subscriptable like sequences.


List vs set in Python

Lists keep every append; sets collapse duplicates silently.

python
xs = [1, 1, 2]
ys = {1, 1, 2}
print(len(xs), len(ys))
Output

You should see 3 then 2. Both support in, but sets target fast membership on large unique collections, while lists scan in average O(n). When you need counts per value, not just uniqueness, Counter examples are often clearer than a bare set. Pick a list for order and duplicates; pick a set for unique elements and fast membership at the cost of order and indexing.


List vs dictionary in Python

Lists map non-negative integers 0..n-1 to values; dicts map keys (strings, tuples, etc.) to values. If your access pattern is “field by name,” a dict reads clearer than parallel lists. When you still need an index while looping a sequence, enumerate() keeps that explicit. Use a list for homogeneous rows or positional meaning; use a dict for named attributes, sparse tables keyed by id, or JSON-like records.

python
# Parallel lists: tie columns together by shared index
ids = [10, 20]
names = ["Ada", "Bob"]
idx = ids.index(20)
print("name from lists:", names[idx])

# Dict: one row, keys carry meaning
user = {"id": 20, "name": "Bob"}
print("name from dict:", user["name"])
Output

You should see name from lists: Bob and name from dict: Bob—same answer, but the dict path avoids hunting the index when you already think in field names.


Tuple and set vs dictionary in Python

Tuples are small positional records; dicts are keyed maps. If you only ever use row[0] and row[1], a dict with "id" and "name" is usually easier to read; tuples stay ideal when the shape is tiny and fixed (for example (x, y)). A set is only keys with no values; a dict attaches a value to each key. If you only need “is this id in the collection?” a set of ids is enough; if you need “id → object”, use a dict. Merge two dicts covers combining mappings without losing keys.

python
point = (3, 4)
print("tuple axis:", point[0], point[1])

allowed_ids = {10, 20, 30}
print("set membership:", 20 in allowed_ids)

by_id = {10: "Ada", 20: "Bob", 30: "Cy"}
print("dict lookup:", by_id[20])
Output

You should see tuple axis: 3 4, then set membership: True, then dict lookup: Bob—tuple for fixed (x, y)-style access, set when only membership matters, dict when each id maps to a value.


Python list vs tuple vs set vs dictionary comparison table

Topic List Tuple Set Dictionary
Literal [1, 2] (1, 2) or (1,) {1, 2} {"a": 1}
Empty [] () set() {}
Ordered iteration Yes Yes Order not part of the abstraction Yes (insertion order, 3.7+)
Duplicates Yes Yes No Duplicate keys forbidden
Indexing lst[i] tup[i] No d[key] (not sequential)
Typical mutability Mutable Immutable Mutable Mutable
Hashable (as dict key) No Sometimes (if all items hashable) No (mutable set) Keys yes; dict itself no

Common operations (add, remove, sort, membership)

Append, extend, and pop illustrate how lists grow and shrink; tuples are reassigned as a whole; sets use add/remove and arbitrary pop behavior; dicts use key assignment and del or pop on keys. Sorting: lists support in-place .sort() or sorted(lst); tuples and sets use sorted(...), which returns a new list without mutating the original tuple or set; sorted(d) sorts dict keys. See sort a list for list patterns.

python
lst = [1]
lst.append(2)

t = (1,)  # new tuple to "add"
s = {1}
s.add(2)
d = {"a": 1}
d["b"] = 2
print(lst, t, s, d)
Output

You should see the list, tuple, set, and dict each reflecting the new values (the tuple still shows (1,) unless you build a longer tuple elsewhere).

python
lst = [1, 2, 3]
lst.pop(0)

s = {1, 2, 3}
s.remove(2)

d = {"a": 1, "b": 2}
del d["a"]
print(lst, s, d)
Output

Tuples have no pop or remove on the same object—you build a new tuple without the unwanted element when you must “remove” logically.

python
data_list = [1, 2, 3]
data_set = {1, 2, 3}
data_dict = {"k": 1}
print(2 in data_list, 2 in data_set, "k" in data_dict)
Output

You should see three True values.


Common mistakes

  • Using {} expecting an empty set—it is an empty dict; use set().
  • Treating a set like s[0]—no indexing; sorted(s)[0] only if that rule truly matches your intent.
  • Assuming set iteration order is stable across runs—do not rely on it for tests or persistence.
  • Putting unhashable values (for example lists) inside a set or as dict keys.
  • Confusing sorted(t) (returns a list) with mutating the original tuple.

Summary and how to choose

Match the container to how you read and write data: growing ordered data with duplicates → list; fixed small record you should not mutate → tuple; unique values and membership-heavy work → set; named fields or arbitrary keys → dict; immutability plus uniqueness → frozenset or a tuple of hashables. Lists and tuples stay ordered sequences (mutable vs immutable); sets give uniqueness without indexing; dicts map keys to values and keep insertion order in modern Python.


References


Frequently Asked Questions

1. What is the main difference between a Python list and a tuple?

Both are ordered sequences and allow duplicates; lists are mutable (you can change elements, append, pop) while tuples are immutable (the tuple object itself cannot be resized or have elements reassigned without building a new tuple).

2. When should I use a set instead of a list in Python?

Use a set when you only care about unique membership and fast lookup, and order either does not matter or you sort explicitly; use a list when you need stable positions, duplicates, or stack-like operations at the end.

3. Is a Python dictionary ordered?

Since Python 3.7 (guaranteed in 3.8+), the built-in dict preserves insertion order when you iterate keys, values, or items; values are looked up by key, not by integer index like a list.

4. Can I use curly braces for both a set and a dict?

Yes—{} is an empty dict; use set() for an empty set, or write a non-empty set like {1, 2} with values only (no colons) to distinguish it from {key: value} dict literals.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …