Kahibaro
Discord Login Register

Choosing the right data structure

Why Choosing the Right Data Structure Matters

In many programs, the choice of data structure affects:

Python gives you several built-in data collections:

Here you’ll focus on how to decide which one to use, not on their basic syntax.

Think of data structures like different containers:

They all hold “stuff,” but each is better for different situations.

Key Questions to Help You Choose

When deciding which data structure to use, ask yourself:

  1. Do I care about order?
  2. Will the data change (add/remove items)?
  3. Do I need to look things up by a number position or by a name/key?
  4. Can there be duplicates, or must all items be unique?
  5. Do I need a fixed “record” that represents one thing (like a user or product)?
  6. Do I want to prevent accidental modification?

Your answers will guide you to lists, tuples, dictionaries, or sets.

When to Use a List

Use a list when you think: “I have a sequence of items, in order, that might change.”

Typical situations:

Good signs you want a list:

Example use cases:

  cart_items = ["apple", "banana", "chocolate"]
  recent_searches = ["python list tutorial", "best laptop", "weather today"]

If your main question is “What’s the item at position N?”, a list is usually the first choice.

When to Use a Tuple

Use a tuple when you think: “I have a small fixed collection of values that belong together and should not change.”

Tuples are good for records: one thing with several fields.

Typical situations:

  point = (10, 20)   # x, y
  birthday = (1995, 7, 30)

Good signs you want a tuple:

Compare:

If your main thought is “These values form one unchanging thing”, consider a tuple.

When to Use a Dictionary

Use a dict when you think: “I want to look up values by a name or key, not by position.”

Dictionaries are like labeled drawers: each value has a label (key).

Typical situations:

  user = {
      "username": "alice",
      "age": 30,
      "email": "alice@example.com"
  }
  word_counts = {"python": 3, "code": 5}
  settings = {
      "volume": 80,
      "theme": "dark",
      "notifications": True
  }

Good signs you want a dictionary:

If your main question is “What is the value for this key?”, choose a dictionary.

When to Use a Set

Use a set when you think: “I want a collection that automatically ignores duplicates and cares only about membership, not order.”

Sets are like bags where:

Typical situations:

Example:

visited_pages = {"home", "about", "contact"}
if "blog" not in visited_pages:
    print("User has not visited the blog page yet")

Good signs you want a set:

If your main question is “Is this item present, and I don’t care about duplicates or order?”, use a set.

Comparing Data Structures by Common Needs

Here’s a quick comparison based on typical needs:

1. Do you need to keep the order?

2. Will the contents change?

3. Do you need named fields?

4. Are duplicates allowed?

5. What’s the main operation?

Real-World Scenario Examples

Example 1: Students in a class

You’re storing information about students.

You might use:

  students = ["Alice", "Bob", "Charlie"]
  student = {
      "name": "Alice",
      "age": 20,
      "id": "S123",
      "grade": "A"
  }
  student_ids = {"S123", "S456", "S789"}
  alice = ("Alice", 20, "S123")

Here you might combine several structures:

  students = [
      {"name": "Alice", "age": 20, "id": "S123"},
      {"name": "Bob", "age": 21, "id": "S124"},
  ]

Example 2: Settings for an application

You’re storing app settings that can be changed during runtime:

  settings = {
      "theme": "dark",
      "volume": 70,
      "language": "en"
  }

Why dict?

A list like ["dark", 70, "en"] is less clear: what does each position mean?

Example 3: Tags on blog posts

You have tags such as "python", "learning", "beginner".

  tags = {"python", "learning", "beginner"}

If you need to store posts with their tags:

post = {
    "title": "Learning Python",
    "tags": {"python", "learning", "beginner"}
}

Here you combine a dict (for named fields) and a set (for unique tags).

Example 4: Ordered steps in a recipe

A recipe has steps that must be followed in order:

  steps = [
      "Preheat the oven to 180°C",
      "Mix flour and sugar",
      "Add eggs",
      "Bake for 25 minutes"
  ]

You might insert a new step or change one, so a list is more flexible than a tuple here.

Simple Decision Guide

You can use this as a quick mental checklist:

  1. Is this a sequence of items, possibly with duplicates, where order matters?
    → Use a list.
  2. Is this a small, fixed-size “record” that forms one unit and should not change?
    → Use a tuple.
  3. Do I need to map names/keys to values (like a mini-database of properties)?
    → Use a dict.
  4. Do I only care about unique items and quick membership tests, not order?
    → Use a set.
  5. Do I need a combination?
    • Lists of dictionaries
    • Dictionaries with sets inside
    • Lists of tuples
      Often, real programs use several data structures together.

Practicing Choosing Data Structures

To get better at this, practice by:

Example practice questions (you don’t have to code them now, just think about the choice):

  1. A playlist of songs where order matters and songs can repeat
  2. A product with a name, price, and stock count
  3. A list of all unique countries users are from
  4. Store each user’s username and their list of favorite movies

Your job is to decide:

The more you think this way, the more natural choosing the right data structure will become.

Views: 15

Comments

Please login to add a comment.

Don't have an account? Register now!