Python Refresher
Data Structures
Data structures are ways to organize data so your program can use it fast and clearly. Picking the right one saves time and prevents bugs.
1. List
A list is an ordered, changeable collection.
1temps = [24.5, 25.1, 26.0]23temps.append(26.3)4print(temps[0])5print(temps[-1])What it is: A flexible sequence of items.
How it is used: Store many values and access by index.
When it is used: Sensor readings, marks list, batched samples.
2. Tuple
A tuple is like a list but immutable (cannot be changed).
1resolution = (1920, 1080)2width, height = resolution3print(width, height)What it is: A fixed ordered collection.
How it is used: Store values that should not change.
When it is used: Coordinates, RGB values, fixed config pairs.
3. Dictionary
A dictionary stores key-value pairs.
1device = {2 "id": "esp32-01",3 "temp": 27.2,4 "active": True5}67print(device["temp"])8device["temp"] = 27.89device["room"] = "lab"What it is: A lookup table.
How it is used: Find data by key instead of index.
When it is used: JSON data, API responses, configuration, metadata.
4. Set
A set stores unique values only.
1names = {"A", "B", "A", "C"}2print(names) # {'A', 'B', 'C'}What it is: An unordered unique collection.
How it is used: Remove duplicates and test membership quickly.
When it is used: Unique IDs, tags, visited nodes.
5. Common Operations You Should Know
Slicing (parts of a list or string)
1nums = [10, 20, 30, 40, 50]2print(nums[1:4]) # [20, 30, 40]3print(nums[:3]) # [10, 20, 30]4print(nums[::2]) # [10, 30, 50]Membership check
1if 30 in nums:2 print("Found")Length
1print(len(nums))6. List Comprehension
Fast way to create lists.
1squares = [x * x for x in range(6)]2print(squares)With condition:
1evens = [x for x in range(20) if x % 2 == 0]2print(evens)What it is: Compact list-building syntax.
How it is used: Transform or filter data in one line.
When it is used: Feature engineering and preprocessing.
7. Dictionary Comprehension
1prices = {"pen": 10, "book": 80, "bag": 500}2with_tax = {k: v * 1.18 for k, v in prices.items()}3print(with_tax)8. Stack and Queue in Python
Stack (LIFO: last in, first out)
1stack = []2stack.append("task1")3stack.append("task2")4print(stack.pop()) # task2Queue (FIFO: first in, first out)
1from collections import deque23queue = deque()4queue.append("student1")5queue.append("student2")6print(queue.popleft()) # student19. Choosing the Right Data Structure
| Situation | Best choice | Why |
|---|---|---|
| Ordered, editable sequence | List | Easy indexing and updates |
| Fixed pair/triple values | Tuple | Safer immutable data |
| Key-based lookup | Dictionary | Fast by key |
| Unique values only | Set | Removes duplicates |
| Undo/backtracking | Stack | LIFO behavior |
| First-come processing | Queue | FIFO behavior |
10. Tiny Complexity Intuition (No heavy math)
- List indexing
arr[i]is usually fast. - Dictionary key lookup is usually very fast.
- Searching in plain list can be slower for big lists.
For beginner projects, readability first. Optimize only when needed.
11. Real Example: Sensor Packet
1packet = {2 "device_id": "esp32-01",3 "time": "2026-05-09T11:30:00Z",4 "readings": [24.8, 25.0, 25.1, 24.9],5 "labels": {"room": "lab", "floor": 2}6}78avg_temp = sum(packet["readings"]) / len(packet["readings"])9print(avg_temp)You used dictionary + list in one structure. This is very common in ML data pipelines.
Practice Tasks
- Remove duplicates from a list using a set.
- Build a dictionary from a list of names and marks.
- Create a queue simulation for customer tokens.
- Extract top 3 values from a list and print average.
Master these and your Python foundation becomes much stronger.

