Lists, Tuples, Sets and Dictionaries

Harry · 14 Sep 2026 · 2 views
Advertisement
Advertisement

Lists

Ordered, mutable sequences - the workhorse collection of Python.

fruits = ["apple", "banana", "mango"]
fruits.append("orange")
fruits.remove("banana")
print(fruits[0])      # apple
print(fruits[-1])     # orange
print(fruits[1:])     # slice from index 1

Tuples

Ordered but immutable - perfect for fixed sets of values.

point = (3, 4)
x, y = point          # unpacking
print(x, y)

Sets

Unordered collections of unique items; great for de-duplication and set maths.

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

Dictionaries

user = {"name": "Priya", "age": 28}
user["city"] = "Mumbai"        # add / update
print(user.get("name"))
print(user.get("missing", "default"))

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

List Comprehensions

squares = [n * n for n in range(6)]        # [0,1,4,9,16,25]
evens = [n for n in range(10) if n % 2 == 0]

Key Points

  • Choose the right tool: list (ordered/mutable), tuple (fixed), set (unique), dict (key-value).
  • Comprehensions build collections in one readable line.
  • Unpacking with a, b = ... is idiomatic.
Share this post:

Comments (0)

Please login or register to comment.