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.

python
1temps = [24.5, 25.1, 26.0]
2
3temps.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).

python
1resolution = (1920, 1080)
2width, height = resolution
3print(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.

python
1device = {
2 "id": "esp32-01",
3 "temp": 27.2,
4 "active": True
5}
6
7print(device["temp"])
8device["temp"] = 27.8
9device["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.

python
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)

python
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

python
1if 30 in nums:
2 print("Found")

Length

python
1print(len(nums))

6. List Comprehension

Fast way to create lists.

python
1squares = [x * x for x in range(6)]
2print(squares)

With condition:

python
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

python
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)

python
1stack = []
2stack.append("task1")
3stack.append("task2")
4print(stack.pop()) # task2

Queue (FIFO: first in, first out)

python
1from collections import deque
2
3queue = deque()
4queue.append("student1")
5queue.append("student2")
6print(queue.popleft()) # student1

9. Choosing the Right Data Structure

SituationBest choiceWhy
Ordered, editable sequenceListEasy indexing and updates
Fixed pair/triple valuesTupleSafer immutable data
Key-based lookupDictionaryFast by key
Unique values onlySetRemoves duplicates
Undo/backtrackingStackLIFO behavior
First-come processingQueueFIFO 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

python
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}
7
8avg_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

  1. Remove duplicates from a list using a set.
  2. Build a dictionary from a list of names and marks.
  3. Create a queue simulation for customer tokens.
  4. Extract top 3 values from a list and print average.

Master these and your Python foundation becomes much stronger.

Previous
Basics & Syntax