Back to website
Mayank Aggarwal Learning Center

Phase 02 · Python FoundationsLesson 4 of 8

Lists, Tuples, Sets & Dictionaries

Python's four built-in collections and when to use each.

  • 10:54 video
  • 3 min read
  • Beginner
01

The problem

Real programs rarely work with one value at a time. You'll handle lists of users, sets of tags, records of products. Python gives you four built-in collections, and choosing the right one makes code simpler, faster and less buggy.

02

The concept

The four collections#

CollectionSyntaxOrdered?Changeable?Duplicates?Best for
List[1, 2, 3]YesYesYesA sequence of items you'll add to or change
Tuple(1, 2, 3)YesNoYesFixed records, e.g. coordinates
Set{1, 2, 3}NoYesNoUnique items and fast membership checks
Dictionary{"a": 1}YesYesKeys: noLooking things up by a key

Note: dictionaries remember the order keys were added (Python 3.7 and later).

Lists: your default collection#

Python
skills = ["Python", "SQL"]
skills.append("Git")       # add to the end
skills[0]                  # 'Python'
skills.remove("SQL")
len(skills)                # 2

Lists are ordered and changeable, so they're the right choice most of the time.

Tuples: fixed records#

Python
location = (28.61, 77.21)   # latitude, longitude
lat, lng = location         # "unpacking"

Because tuples can't change, they're safe for data that shouldn't be modified by accident, and they can be used as dictionary keys.

Sets: uniqueness#

Python
tags = {"ai", "python", "ai"}
print(tags)          # {'ai', 'python'}  (duplicate removed)
"python" in tags     # True, and very fast even for huge sets

a = {"python", "sql"}
b = {"python", "docker"}
a & b    # {'python'}               (in both)
a | b    # {'python', 'sql', 'docker'}  (in either)

Dictionaries: key → value#

Python
student = {"name": "Ada", "score": 91}
student["score"]                 # 91
student["city"] = "Pune"         # add a new key
student.get("age", "unknown")    # 'unknown' (safe lookup)

for key, value in student.items():
    print(key, value)

List comprehensions#

A compact way to build a new list from an existing one:

Python
nums = [-2, 3, -1, 5]
squares = [n * n for n in nums if n > 0]   # [9, 25]

# The same thing with a regular loop:
squares = []
for n in nums:
    if n > 0:
        squares.append(n * n)

Choosing the right one#

  • Need order and changes? List.
  • Data that must never change? Tuple.
  • Need uniqueness or fast "is it in here?" checks? Set.
  • Need to look something up by name or ID? Dictionary.

Which collection?

  1. What does your data need?
  • order and changes
    1. List[1, 2, 3]
  • must never change
    1. Tuple(1, 2, 3)
  • uniqueness or fast "is it in here?"
    1. Set{1, 2, 3}
  • lookup by name or ID
    1. Dictionary{"a": 1}
03

Try it

Your task · about 10 minutes

Given a list of words with repeats, print the unique words (set) and how many times each appears (dict).

Use the interactive visual above to explore it step by step
04

Recap & next

Key takeaways

  1. List: ordered and mutable. Your default collection ([1, 2, 3]).
  2. Tuple: ordered and immutable. Good for fixed records such as (lat, lng).
  3. Set: unordered and unique. Removes duplicates and makes membership checks fast.
  4. Dictionary: key → value mapping for fast lookups ({"name": "Ada"}).
  5. List comprehensions build lists concisely: [x * x for x in nums if x > 0].

Quick check

2 questions · instant feedback

1Which collection removes duplicates automatically?
2Which is best for looking up a value by a key?

Finished this lesson?

Mark it complete to track your progress and keep your streak going.

Next