Jan 26, 2026
# Chapter 6 — Lists, Tuples, Sets, and Dicts (The Daily Tools)
This chapter is written like a cheat-sheet + mini exercises.
## 6.1 List
```python
scores = [10, 20, 30]
scores.append(40)
print(scores[0])
```
## 6.2 Tuple (Fixed Collection)
```python
point = (3, 4)
print(point[1])
```
## 6.3 Set (Unique Items)
```python
names = {"a", "b", "a"}
print(names)
```
## 6.4 Dict (Key → Value)
```python
user = {"name": "Riya", "site": "meetcode"}
print(user["name"])
user["role"] = "student"
print(user)
```
### Diagram: Dict as a Map
```text
name -> "Riya"
site -> "meetcode"
role -> "student"
```
## 6.5 Lists Are Mutable (Most Important List Fact)
Mutable means: you can change the same list object.
```python
a = [1, 2, 3]
b = a
a.append(4)
print(a, b)
```
Both show the change, because both names point to the same list.
### Diagram: Two Labels, One List
```text
a ----
---> [1, 2, 3, 4]
b ----/
```
## 6.6 Copying Lists (Shallow Copy, Common Trap)
If you want a new list object:
```python
a = [1, 2, 3]
b = list(a)
a.append(4)
print(a, b)
```
But if a list contains other lists, you must be careful:
```python
grid = [[1, 2], [3, 4]]
copy1 = list(grid)
grid[0].append(99)
print(grid, copy1)
```
The inner lists are still shared. That is why people call it a “shallow” copy.
## 6.7 Slicing (Readable and Powerful)
```python
nums = [10, 20, 30, 40, 50]
print(nums[1:4])
print(nums[:3])
print(nums[::2])
```
## 6.8 List Comprehension (Clean Loops)
```python
nums = [1, 2, 3, 4, 5]
squares = [n * n for n in nums]
evens = [n for n in nums if n % 2 == 0]
print(squares, evens)
```
## 6.9 Sorting (Stable, Key-Based, Real-World)
```python
students = [
{"name": "Asha", "score": 90},
{"name": "Ravi", "score": 75},
{"name": "Meera", "score": 90},
]
sorted_students = sorted(students, key=lambda s: (-(s["score"]), s["name"]))
print(sorted_students)
```
Sorting becomes easy when you think: “What key should decide the order?”
## 6.10 Dict Patterns You Use Daily
### Safe read with default
```python
user = {"name": "Asha"}
print(user.get("role", "student"))
```
### Counting with a dict (classic interview + real work)
```python
text = "meetcode"
count = {}
for ch in text:
count[ch] = count.get(ch, 0) + 1
print(count)
```
---