๐Ÿ“‹ 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.

Key idea: Lists are ordered, mutable, and indexed from 0. You can modify them after creation.

๐Ÿ“ฆ Lists vs Tuples

list
[1, 2, 3]
Mutable, ordered. Change it anytime.
tuple
(1, 2, 3)
Immutable, ordered. Fixed forever.

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

๐Ÿฆ
Real-World Grounding
SouthBank India โ€” Account Management in Mumbai

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.

โ‚น177,500
Recent Balance
12
Txns This Week
Mumbai
Branch City

๐Ÿ”ง 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.

Python Create & index

๐Ÿ”„ 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.

Python List methods

โœ‚๏ธ 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.

Python Slicing & unpacking

๐Ÿ—๏ธ 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.

Python Nesting & named tuples

๐ŸŒ Also used inโ€ฆ

Real-world domains

๐Ÿ’ณ
customer_txns = [...]
Banking: store and sort transaction history; tuples for immutable account metadata
๐Ÿ“Š
prices = [45.50, 32.99, ...]
E-commerce: list of product prices (sortable); tuple of coordinates (lat, lon)
๐ŸŽฎ
scores = [100, 150, 200]
Gaming: leaderboards use lists; fixed player positions stored in tuples

๐Ÿค– 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.

batch processing nested arrays immutable configs

โšก Mini challenge: Build a transaction analyzer

Task: Given a list of transactions [12000, 5500, 8200, 3000, 15400], write code to:
  1. Remove the smallest transaction
  2. Add a new transaction (โ‚น6700) to the end
  3. Sort the remaining transactions in descending order
  4. Print the top 3 transactions
Use .min() to find smallest, then .remove() it. Use .sort(reverse=True) for descending. Slice with [0:3] for top 3.

๐Ÿ’ผ Interview questions

Lists are mutable โ€” you can modify them after creation using append, remove, sort, etc. Tuples are immutable โ€” once created, they cannot be changed. Use lists for data that evolves (transaction logs); use tuples for fixed data (coordinates, account info). Tuples are also slightly faster and can be used as dictionary keys, while lists cannot.
Lists are 0-indexed: the first item is at index 0, the second at index 1, etc. Negative indices count from the end: -1 is the last item, -2 is second-to-last. So list[-1] always gets the last element. This is convenient for accessing elements without knowing the list length.
Slicing extracts a subset. 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.
Use tuples when the data should never change. Examples: coordinates (latitude, longitude), fixed account info (account number, holder name), or function return values with multiple fixed outputs. Tuples are faster, safer (no accidental modification), and can be dictionary keys (lists cannot). If you need to add, remove, or sort, use a list.
Tuples themselves are immutable โ€” you cannot reassign tuple[0] or add items. However, if a tuple contains a mutable object (like a list), you can modify that inner object. For example, 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

Question 1 of 5
What is the index of the first item in a list?
Question 2 of 5
What does list.append(item) do?
Question 3 of 5
Which is immutable: list or tuple?
Question 4 of 5
What does list[1:4] return?
Question 5 of 5
Can you use a list as a dictionary key?
0
/5

๐Ÿ“‹ Quick Cheatsheet

OperationListTupleNotes
Create[1, 2, 3](1, 2, 3)[] for list, () for tuple
Index accesslist[0]tuple[0]Both 0-indexed; -1 is last
Slicelist[1:3]tuple[1:3]Both support slicing
Appendlist.append(x)n/aTuples cannot be modified
Removelist.remove(x)n/aTuples are immutable
Sortlist.sort()n/aLists only; in-place
Lengthlen(list)len(tuple)Both support len()
Unpackinga, b = [1, 2]a, b = (1, 2)Tuples more common

โš ๏ธ Common mistakes

Forgetting 0-indexing
Python lists start at 0. list[1] is the second item, not the first. Use list[0] for the first item.
Confusing slice end behavior
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".
Trying to modify a tuple
Tuples are immutable. tuple[0] = 5 raises TypeError. If you need to change data, convert to a list: list(tuple_var), modify, then convert back.
Using a list as a dictionary key
Lists are mutable and unhashable, so they cannot be dictionary keys. Use a tuple instead: 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 of x = 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
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

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 โ†’