Loading content...
Loading content...
Master organizing and manipulating datasets: ordered mutable lists, immutable tuples, key-value dictionaries, indexing, operations (append, remove, del, len), and structured nested records.
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.(): Ordered, permanent (immutable) sequence.: Key-value mapping for labeled attributes.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 |
Practice list indexing, mutation, appending, and removal:
print(sales[0])print(sales[-1])sales[0] = 50000sales.append(60000)sales.remove(32000)print(sales) and print(len(sales))In data analytics, lists store multiple records, and loops process each record sequentially:
A tuple is defined using parentheses (). Like lists, tuples are ordered and 0-indexed, but cannot be modified after creation:
| 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) |
Run the code below to see the authentic TypeError Python raises when attempting to alter a tuple:
A dictionary stores data in key: value pairs using curly braces {}:
Modify, insert, access, and delete key-value pairs in a dictionary:
print(student["name"])student["marks"] = 90student["city"] = "Mumbai"del student["age"]print(student)| 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"] |
In real data analytics, data structures nest inside each other. A table of rows is represented as a list of dictionaries:
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).
Process a list of student records, extract dictionary fields, and identify high performers:
| 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. |
Test your mastery of Python lists, tuples, and dictionaries:
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)?