Python Collections: Lists, Tuples & Dictionaries
Master organizing and manipulating datasets: ordered mutable lists, immutable tuples, key-value dictionaries, indexing, operations (append, remove, del, len), and structured nested records.
1. Why Do We Need Collections?
Storing related values in separate variables is unmanageable when working with real datasets:
name1 = "Amit" name2 = "Priya" name3 = "Rahul" # Cannot easily loop, filter, or sort!
names = ["Amit", "Priya", "Rahul"] # Store, loop, slice, and mutate in 1 object!
[]: Ordered, mutable sequence of values.2. Tuple
(): Ordered, permanent (immutable) sequence.3. Dictionary
: Key-value mapping for labeled attributes.2. Lists: Ordered & Mutable
A list is created using square brackets []. Python lists are 0-indexed:
| Operation | Python Code | Result / Behavior |
|---|---|---|
| Access first item | names[0] | Returns "Amit" |
| Access last item | names[-1] | Returns "Rahul" |
| Modify element | names[0] = "Sumit" | Replaces first element (mutable) |
| Append new item | names.append("Neha") | Adds to the end of the list |
| Remove item | names.remove("Priya") | Deletes the specified value |
| Count elements | len(names) | Returns total number of items |
3. 🔥 Live Lab: List Manipulation
Practice list indexing, mutation, appending, and removal:
- Print first value:
print(sales[0]) - Print last value:
print(sales[-1]) - Change first value to 50000:
sales[0] = 50000 - Append 60000:
sales.append(60000) - Remove 32000:
sales.remove(32000) - Print updated list and length:
print(sales)andprint(len(sales))
4. Lists + Loops: The Core Data Processing Unit
In data analytics, lists store multiple records, and loops process each record sequentially:
5. Tuples: Ordered & Immutable
A tuple is defined using parentheses (). Like lists, tuples are ordered and 0-indexed, but cannot be modified after creation:
print(coordinates[0]) # Accesses 19.07
print(len(coordinates)) # Returns 2
6. List vs Tuple: Head-to-Head Comparison
| Property | List [] | Tuple () |
|---|---|---|
| Mutability | Mutable (Can add, remove, change items) | Immutable (Fixed after creation) |
| Syntax | Square brackets: [1, 2, 3] | Parentheses: (1, 2, 3) |
| When to Choose | When the data will grow, shrink, or be sorted | When the data represents a fixed record or constant |
| Examples | Sales figures, customer names, transactions | GPS coordinates (lat, lon), RGB colors (255, 0, 0) |
7. 🔥 Live Lab: Observing Tuple Immutability in Action
Run the code below to see the authentic TypeError Python raises when attempting to alter a tuple:
8. Dictionaries: Key-Value Mappings
A dictionary stores data in key: value pairs using curly braces {}:
"name": "Amit",
"age": 22,
"marks": 85
}
print(student["name"]) # Access by key → "Amit"
9. 🔥 Live Lab: Dictionary Operations
Modify, insert, access, and delete key-value pairs in a dictionary:
- Print student name:
print(student["name"]) - Change marks to 90:
student["marks"] = 90 - Add a new key "city":
student["city"] = "Mumbai" - Delete "age":
del student["age"] - Print the updated dictionary:
print(student)
10. List vs Tuple vs Dictionary: The Master Matrix
| Structure | Stores | Ordered? | Mutable? | Access Method |
|---|---|---|---|---|
List [] | Multiple values | Yes | Yes | Numeric Index: list[0] |
Tuple () | Multiple values | Yes | No | Numeric Index: tuple[0] |
Dictionary {} | Key-Value pairs | Yes (Python 3.7+) | Yes | By Key: dict["key"] |
11. 🔥 Scenario Chooser: When to Use What?
12. Nested Data: Real-World Datasets
In real data analytics, data structures nest inside each other. A table of rows is represented as a list of dictionaries:
{"name": "Amit", "marks": 85},
{"name": "Priya", "marks": 92}
]
13. Practical Data Analytics: Records & Tables
Notice the standard architectural duality:
[]Used to hold multiple records (e.g., 10,000 transactions).
{}Used to describe one record with named column fields (product, price, qty).
14. 🔥 Final Live Challenge: Student Analytics
Process a list of student records, extract dictionary fields, and identify high performers:
15. Common Beginner Mistakes Checklist
| Mistake | Incorrect Code | Correct Code | Why It Fails |
|---|---|---|---|
| 1-based indexing assumption | first = names[1] | first = names[0] | Python sequence indexing starts strictly at 0. |
| Trying to modify a tuple | tuple[0] = 5 | Use a list instead | Tuples are immutable; throws TypeError. |
| Using index on dictionary | student[0] | student["name"] | Dictionaries are indexed by keys, not integer positions. |
| Misspelling dictionary key | student["nam"] | student["name"] | Non-existent keys throw KeyError. |
16. Comprehensive Knowledge Assessment
Test your mastery of Python lists, tuples, and dictionaries:
Python Collections Mastery Quiz
Test your understanding of list mutability, tuple immutability, dictionary key-value access, and Python 3.7+ ordering guarantees.
1. What error does Python raise when you attempt to modify an element in a tuple (e.g., t = (10, 20); t[0] = 99)?