Lists & Tuples

0/5 in this phase0/54 across the roadmap

📖 Concept

Lists and tuples are Python's primary ordered sequence types. Lists are mutable (can be modified after creation), while tuples are immutable (cannot be changed). This distinction has important implications for performance, safety, and usage patterns.

When to use each:

Use Case List Tuple
Collection of similar items Yes Possible but less common
Record of different fields Possible but less common Yes
Dictionary key No (unhashable) Yes (hashable)
Function return value (multiple) Possible Yes (idiomatic)
Needs modification Yes No (create new tuple)
Memory efficiency Less More
Thread safety No (needs locking) Yes (immutable)

List comprehensions are a Pythonic way to create lists from existing iterables. They're more readable and often faster than equivalent for loops with .append().

# Syntax: [expression for item in iterable if condition]
squares = [x**2 for x in range(10)]
evens = [x for x in range(20) if x % 2 == 0]

Slicing is Python's powerful way to extract subsequences: sequence[start:stop:step]. It works on lists, tuples, strings, and any sequence type.

Unpacking lets you assign sequence elements to multiple variables in one line: a, b, c = [1, 2, 3]. The * operator captures remaining elements.

💻 Code Example

codeTap to expand ⛶
1# ============================================================
2# List creation and operations
3# ============================================================
4# Creating lists
5empty = []
6numbers = [1, 2, 3, 4, 5]
7mixed = [1, "hello", 3.14, True, None] # Any types
8nested = [[1, 2], [3, 4], [5, 6]]
9
10# List from other iterables
11from_range = list(range(5)) # [0, 1, 2, 3, 4]
12from_string = list("hello") # ['h', 'e', 'l', 'l', 'o']
13from_generator = list(x**2 for x in range(5)) # [0, 1, 4, 9, 16]
14
15# ============================================================
16# List methods (modify in-place)
17# ============================================================
18fruits = ["apple", "banana"]
19
20fruits.append("cherry") # Add to end → ['apple', 'banana', 'cherry']
21fruits.insert(1, "blueberry") # Insert at index → ['apple', 'blueberry', 'banana', 'cherry']
22fruits.extend(["date", "fig"]) # Add multiple items
23
24fruits.remove("banana") # Remove by value (first occurrence)
25popped = fruits.pop() # Remove and return last item
26popped_at = fruits.pop(0) # Remove and return at index
27
28fruits.sort() # Sort in-place (returns None!)
29fruits.sort(reverse=True) # Sort descending
30fruits.reverse() # Reverse in-place
31
32idx = fruits.index("cherry") # Find index of value
33count = fruits.count("cherry") # Count occurrences
34
35# IMPORTANT: sort() returns None, not the sorted list!
36# BAD: sorted_list = my_list.sort() # sorted_list is None!
37# GOOD: sorted_list = sorted(my_list) # Returns new sorted list
38
39# ============================================================
40# Slicing [start:stop:step]
41# ============================================================
42nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
43
44print(nums[2:5]) # [2, 3, 4] — index 2 to 4 (stop is exclusive)
45print(nums[:3]) # [0, 1, 2] — first 3 elements
46print(nums[7:]) # [7, 8, 9]from index 7 to end
47print(nums[-3:]) # [7, 8, 9] — last 3 elements
48print(nums[::2]) # [0, 2, 4, 6, 8] — every 2nd element
49print(nums[::-1]) # [9, 8, 7, ..., 0] — reversed
50print(nums[1:8:2]) # [1, 3, 5, 7] — every 2nd from index 1 to 7
51
52# Slice assignment (lists only — tuples are immutable)
53nums[2:5] = [20, 30, 40] # Replace slice
54print(nums) # [0, 1, 20, 30, 40, 5, 6, 7, 8, 9]
55
56# Delete slice
57del nums[2:5]
58print(nums) # [0, 1, 5, 6, 7, 8, 9]
59
60# ============================================================
61# List comprehensions
62# ============================================================
63# Basic comprehension
64squares = [x**2 for x in range(10)]
65print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
66
67# With condition (filter)
68even_squares = [x**2 for x in range(10) if x % 2 == 0]
69print(even_squares) # [0, 4, 16, 36, 64]
70
71# With if-else (transform, not filter)
72labels = ["even" if x % 2 == 0 else "odd" for x in range(5)]
73print(labels) # ['even', 'odd', 'even', 'odd', 'even']
74
75# Nested comprehension (flattening)
76matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
77flat = [num for row in matrix for num in row]
78print(flat) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
79
80# Matrix transpose with nested comprehension
81transposed = [[row[i] for row in matrix] for i in range(3)]
82print(transposed) # [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
83
84# ============================================================
85# Unpacking
86# ============================================================
87# Basic unpacking
88a, b, c = [1, 2, 3]
89print(a, b, c) # 1 2 3
90
91# Star unpacking (Python 3)
92first, *middle, last = [1, 2, 3, 4, 5]
93print(first) # 1
94print(middle) # [2, 3, 4]
95print(last) # 5
96
97head, *tail = [1, 2, 3, 4, 5]
98print(head) # 1
99print(tail) # [2, 3, 4, 5]
100
101# Swap variables
102x, y = 10, 20
103x, y = y, x # Swap without temp variable!
104
105# Ignore values with _
106_, second, _, fourth, _ = [10, 20, 30, 40, 50]
107print(second, fourth) # 20 40
108
109# ============================================================
110# Tuples — immutable sequences
111# ============================================================
112point = (3, 4)
113rgb = (255, 128, 0)
114single = (42,) # Note the comma! (42) is just 42 in parentheses
115
116# Tuple unpacking in functions
117def get_user():
118 return "Alice", 30, "NYC" # Returns a tuple (parens optional)
119
120name, age, city = get_user()
121
122# Named tuples — tuples with field names
123from collections import namedtuple
124Point = namedtuple('Point', ['x', 'y'])
125p = Point(3, 4)
126print(p.x, p.y) # 3 4 (access by name)
127print(p[0], p[1]) # 3 4 (access by index)
128
129# Tuples as dictionary keys (lists can't be keys)
130locations = {
131 (40.7128, -74.0060): "New York",
132 (51.5074, -0.1278): "London",
133}
134print(locations[(40.7128, -74.0060)]) # "New York"
135
136# ============================================================
137# Copying: shallow vs deep
138# ============================================================
139import copy
140
141original = [[1, 2], [3, 4]]
142shallow = original.copy() # or list(original) or original[:]
143deep = copy.deepcopy(original)
144
145original[0][0] = 99
146
147print(shallow) # [[99, 2], [3, 4]] — inner lists are shared!
148print(deep) # [[1, 2], [3, 4]] — completely independent

🏋️ Practice Exercise

Exercises:

  1. Use list comprehension to generate a list of all Pythagorean triples (a, b, c) where a, b, c <= 30 and a^2 + b^2 = c^2.

  2. Implement a function rotate_list(lst, k) that rotates a list by k positions to the right. [1,2,3,4,5] rotated by 2 → [4,5,1,2,3]. Use slicing.

  3. Write a function that takes a list of strings and returns only the palindromes, sorted by length. Use list comprehension.

  4. Demonstrate the difference between shallow copy and deep copy with a nested list. Modify the inner list and show which copy is affected.

  5. Use unpacking to swap two variables, extract the first and last elements of a list, and split a list into head and tail.

  6. Create a 5x5 identity matrix using nested list comprehensions. Then transpose it.

  7. Implement a simple stack (LIFO) and queue (FIFO) using only list operations (append, pop).

⚠️ Common Mistakes

  • Confusing sort() (in-place, returns None) with sorted() (returns new list). Writing result = my_list.sort() gives result = None.

  • Modifying a list while iterating: for item in lst: if condition: lst.remove(item) skips items. Use list comprehension or iterate over a copy.

  • Creating a list of lists with [[]] * 5 — this creates 5 references to the SAME list. Use [[] for _ in range(5)] for independent inner lists.

  • Forgetting the comma in single-element tuples: t = (42) is just int(42), not a tuple. Use t = (42,) or t = 42,.

  • Using .append() when you mean .extend(): [1,2].append([3,4]) gives [1, 2, [3, 4]], not [1, 2, 3, 4]. Use .extend() or + to concatenate.

💼 Interview Questions

🎤 Mock Interview

Practice a live interview for Lists & Tuples