Lists & Tuples: Storing Data
Organizing collections of information and protecting sensitive datasets from accidental changes.
Until now, we have been working with single variablesβone name, one price, one status. But as a Data Analyst, you will never work with one of anything. You will work with thousands.
Imagine you are tracking daily temperatures in Cairo for a full week. Instead of creating seven different variables (day1, day2, etc.), we store them in a single collection. In Python, we have two primary "containers" for this: Lists and Tuples. Choosing the right one is the difference between a flexible script and a protected, crash-proof one.
When you create a List in Python, the variable doesn't actually "hold" the data. Instead, it holds a Pointer to a specific address in your computer's memory where that data lives.
This leads to the "Reference Trap": when you assign an existing list to a new variable (list_b = list_a), Python doesn't create a new box of data. It simply gives you a second pointer to the exact same memory address.
original_sales = [100, 200, 300] new_sales = original_sales # Both variables now point to the same memory ID! # Changing the data via the second pointer new_sales.append(400) # The original list is changed too! print(original_sales) # [100, 200, 300, 400]
Because both variables point to the same object, any change made to one will reflect in the other. To create a completely independent copy, you must use .copy() or the slice trick list_a[:].
In 2D data (like a spreadsheet), we expect every row to have the same number of columns. However, Python lists don't enforce this. We call a list of lists where rows have different lengths a Jagged Array.
If your "Row 1" has 3 values and "Row 2" has 5, your analysis script will likely crash when it tries to calculate an average for "Column 4" because that index simply doesn't exist in the first row.
A Tuple is exactly like a list, but it is Immutableβonce it is created, you cannot add, remove, or change its items. This makes them perfect for "Golden Records" like GPS coordinates for a warehouse in Giza.
This is a common exam and interview question! If you want to create a tuple with only one item, you MUST include a trailing comma: single_item = (5,). Without that comma, Python sees this as a simple integer inside parentheses and you lose all tuple functionality.
# A list of tuples (A robust way to store table rows) students = [ (101, "Mona", 92.5), (102, "Omar", 88.0) ] # students[0][2] = 95.0 # This would throw a TypeError! (Safety)
Let's navigate a 2D dataset representing store inventory.
# A 2D List (Rows = Branches, Columns = Product Counts) inventory = [ [50, 12, 30], # Maadi Branch [20, 8, 45] # Heliopolis Branch ] # 1. Accessing a specific point [Row][Column] maadi_product_b = inventory[0][1] # 12 # 2. Modifying data safely # To avoid the Reference Trap, we copy the row before editing heliopolis_data = inventory[1].copy() heliopolis_data[2] = 50 # 3. Protecting the final result # After cleaning, we convert the row to a tuple to "lock" it final_report_row = tuple(heliopolis_data)
- Lists are mutable; Tuples are immutable (read-only).
- Indexing [row][col] allows you to navigate 2D tables of data.
- The Reference Trap happens because variables are pointers to memory addresses.
- Single-item Tuples require a trailing comma
(x,)to be valid.
-
β
Lists and Tuples in Python (Real Python)
https://realpython.com/python-lists-tuples/ -
β
Python's Mutable vs Immutable Types (Programiz)
https://www.programiz.com/python-programming/mutable-vs-immutable -
β
Python shallow and deep copy operations (Official Docs)
https://docs.python.org/3/library/copy.html