๐ What is a list?
A list is an ordered container that holds multiple values. Unlike a variable that stores one thing, a list can store 10, 100, or 1000 items โ all in one place. Lists are mutable, meaning you can add, remove, or change items after creating them.
Think of a list as a shopping cart. You start with an empty cart, add items one by one, rearrange them, remove what you don't need, and check them out. Each item has a position (index), starting from 0.
๐ฆ Lists vs Tuples
Lists are mutable: You can append, remove, or sort them after creation. Use lists when your data changes โ like a transaction log where new entries arrive constantly.
Tuples are immutable: Once created, you cannot change them. They are faster and safer for fixed data โ like account coordinates or fixed account metadata.
๐ฆ Business Scenario: SouthBank India
SouthBank India needs to track customer transactions in real time. Each customer has a list of recent transactions (which grows daily), plus fixed account information like account number and holder name. The transaction list changes constantly, but the account info stays the same.
๐ง Step 1: Create lists & access by index
A list is created with square brackets []. Each item has an index: 0 is the first, 1 is the second, and so on. Negative indexing works too: -1 is the last item, -2 is second-to-last.
๐ Step 2: Mutate lists โ append, insert, remove, sort
Lists support many methods. append() adds one item to the end. insert() puts an item at a specific index. remove() deletes by value. pop() removes and returns an item. sort() orders the list. All modify the original list in-place.
โ๏ธ Step 3: Slicing and tuple unpacking
Slicing extracts a subset of a list. Use list[start:end] to grab items from index start up to (but not including) end. Tuple unpacking assigns tuple values to separate variables instantly, without indexing.
๐๏ธ Step 4: Nested lists and named tuples
A nested list is a list inside a list โ useful for storing complex data like all transactions for multiple customers. Named tuples are tuples with named fields instead of just indices. They are cleaner than unpacking.
๐ Also used inโฆ
Real-world domains
๐ค Why AI engineers care
Every time a machine learning model processes a batch of data, it's working with lists. Training data is a list of samples. Predictions are lists of probabilities. Neural networks store weights in nested lists. Tuples show up when storing immutable hyperparameters โ learning rates, layer sizes โ that should never change mid-training.
โก Mini challenge: Build a transaction analyzer
[12000, 5500, 8200, 3000, 15400], write code to:
- Remove the smallest transaction
- Add a new transaction (โน6700) to the end
- Sort the remaining transactions in descending order
- Print the top 3 transactions
.min() to find smallest, then .remove() it. Use .sort(reverse=True) for descending. Slice with [0:3] for top 3.
๐ผ Interview questions
list[-1] always gets the last element. This is convenient for accessing elements without knowing the list length.
list[start:end:step] means: start at index start, go up to (but not including) end, and grab every step-th item. list[1:4:2] starts at index 1, goes to 4 (exclusive), grabbing every 2nd item. So from [0,1,2,3,4], you get [1,3]. If step is omitted, it defaults to 1. Negative indices work too: list[::-1] reverses the list.
t = ([1,2], 3); t[0].append(4) is allowed โ the tuple itself didn't change, but its list contents did. True immutability for the entire structure requires all nested items to be immutable too.
๐ฏ Quick Quiz
list.append(item) do?list[1:4] return?๐ Quick Cheatsheet
| Operation | List | Tuple | Notes |
|---|---|---|---|
| Create | [1, 2, 3] | (1, 2, 3) | [] for list, () for tuple |
| Index access | list[0] | tuple[0] | Both 0-indexed; -1 is last |
| Slice | list[1:3] | tuple[1:3] | Both support slicing |
| Append | list.append(x) | n/a | Tuples cannot be modified |
| Remove | list.remove(x) | n/a | Tuples are immutable |
| Sort | list.sort() | n/a | Lists only; in-place |
| Length | len(list) | len(tuple) | Both support len() |
| Unpacking | a, b = [1, 2] | a, b = (1, 2) | Tuples more common |
โ ๏ธ Common mistakes
list[1] is the second item, not the first. Use list[0] for the first item.list[1:4] includes indices 1, 2, 3 but NOT 4. The end index is exclusive. Write it as "go from 1 up to (but not including) 4".tuple[0] = 5 raises TypeError. If you need to change data, convert to a list: list(tuple_var), modify, then convert back.my_dict[(1, 2)] = value, not my_dict[[1, 2]] = value.โฆ Key Takeaways
- Lists are ordered, mutable, and indexed from 0. Use them for data that changes (transaction logs, to-do lists)
- Tuples are ordered, immutable, and indexed from 0. Use them for fixed data (coordinates, account info)
- Negative indices count from the end:
list[-1]is always the last item - Slicing
[start:end:step]extracts a subset; the end index is exclusive - Common list methods:
append(),insert(),remove(),pop(),sort(),reverse() - Tuple unpacking is cleaner than indexing:
x, y = (1, 2)instead ofx = tuple[0]; y = tuple[1] - Nested lists are lists inside lists โ useful for storing complex data (customer transactions per account)
- Named tuples let you access fields by name instead of index, making code more readable
You've mastered lists & tuples.
Now build with real data structures.
Join the Best AI Program: The AI-Era Analyst โ 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Analyst โ