Python Lists, Tuples, Sets and Dictionaries Explained

Part 4 of the Python for Everyone track. Last updated: September 2026.

Python's four built-in collections cover nearly every data-shaping need. The trick is knowing which one fits: need order? need uniqueness? need key lookup? This post gives you all four with the decision rule for each.

Lists — ordered, changeable, allow duplicates

fruits = ["apple", "banana", "cherry"]
fruits.append("mango")       # add to end
fruits.insert(1, "kiwi")     # insert at index
fruits.remove("banana")      # remove by value
last = fruits.pop()          # remove + return last item
fruits.sort()                # sort in place
fruits.reverse()             # reverse in place
print(len(fruits), fruits[0], fruits[-1])

Tuples — ordered, unchangeable, allow duplicates

point = (10, 20)
x, y = point          # unpacking — tuples shine here
print(x, y)           # 10 20

# point[0] = 99       # TypeError — immutable
# Use tuples for fixed data: coordinates, RGB colors, DB rows

Tuples are slightly faster and use less memory than lists — and their immutability makes them safe as dictionary keys.

Sets — unordered, no duplicates

tags = {"python", "java", "python"}
print(tags)  # {'python', 'java'} — duplicate removed automatically

tags.add("go")
tags.discard("java")   # discard won't error if missing (remove() would)

a = {1, 2, 3}
b = {3, 4, 5}
print(a | b)  # {1, 2, 3, 4, 5} — union
print(a & b)  # {3}             — intersection
print(a - b)  # {1, 2}          — difference

Sets are the fastest way to deduplicate a list: list(set(my_list)).

Dictionaries — key-value pairs

user = {"name": "Ada", "age": 36, "admin": True}
print(user["name"])              # Ada
print(user.get("email"))         # None — get() won't crash on missing keys
print(user.get("email", "n/a"))  # "n/a" — with a default

user["age"] = 37                 # update
user["city"] = "London"          # add
del user["admin"]                # delete

for key, value in user.items():  # the standard iteration pattern
    print(f"{key} → {value}")

Which one do I use?

  • List — a sequence you'll add to, remove from, or reorder.
  • Tuple — fixed data that should never change (safer, hashable).
  • Set — membership tests ("is X in there?") and deduplication.
  • Dict — lookup by key instead of position.

Nesting them

students = [
    {"name": "Ada", "scores": [95, 88]},
    {"name": "Grace", "scores": [92, 99]},
]
print(students[1]["scores"][0])  # 92

Real-world data is nested like this — lists of dicts is the shape of most JSON APIs.

Key takeaways

  • List: mutable sequence. Tuple: immutable sequence. Set: unique items. Dict: key lookup.
  • Use .get() on dicts to avoid KeyError on missing keys.
  • list(set(x)) deduplicates; a & b intersects sets.

Next in this series: Python If-Else, Loops and Comprehensions.

Comments

Popular posts from this blog

Java Banking Finance Services and Insurance (BFSI) domain interview questions

JSP Servlet Interview Questions For Freshers Series 1

Java program to check even or odd number