Python
Data Structures
Dictionary
List
Set

In Python, when to use a Dictionary, List or Set?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Choosing between a list, dict, and set in Python is less about memorizing definitions and more about asking what operations your code needs to be good at. Do you care about order, key-based lookup, uniqueness, or membership testing. Once you answer that, the right data structure is usually obvious, and your code becomes both clearer and faster.

Use a list for Ordered Sequences

A list is the default choice when you have an ordered collection of items and duplicates are allowed.

python
names = ["Ada", "Grace", "Linus"]
print(names[0])
names.append("Margaret")

A list is a good fit when:

  • order matters
  • you need index-based access
  • duplicates are acceptable
  • you will iterate through items in sequence

Lists are great for task queues, ordered results, and general-purpose collections.

Use a dict for Key-to-Value Mapping

A dictionary stores values by key. Reach for a dict when you want to answer questions like "what is the value for this id" or "give me the settings for this name."

python
1user = {
2    "id": 101,
3    "name": "Ada",
4    "is_admin": True,
5}
6
7print(user["name"])

A dictionary is the right tool when:

  • each item has a unique key
  • fast lookup by key matters
  • the data is naturally labeled rather than position-based
  • you want to associate one thing with another

In application code, dictionaries are everywhere because most business data is keyed in some way.

Use a set for Uniqueness and Fast Membership Checks

A set stores unique elements only. It is ideal when you care about whether something is present, but not about order or duplicates.

python
visited = {"A", "B", "C"}
print("B" in visited)
visited.add("D")

A set is a good fit when:

  • duplicates should disappear automatically
  • fast membership checks matter
  • mathematical set operations are useful

For example, finding common items is easy:

python
backend = {"python", "go", "java"}
frontend = {"javascript", "typescript", "go"}
print(backend & frontend)

A Quick Way to Decide

Ask these questions in order.

Do I need key-based lookup. If yes, use a dict.

Do I need uniqueness and fast membership testing, but not keyed values. If yes, use a set.

Do I mainly need ordered items, indexing, or duplicates. If yes, use a list.

That decision process covers most ordinary Python code.

Performance Intuition

You do not need to memorize every implementation detail, but a little intuition helps:

  • 'list is strong for ordered iteration and index access'
  • 'dict is strong for key lookup'
  • 'set is strong for membership tests and uniqueness'

People often start with a list because it is familiar, then later realize they are doing repeated x in my_list checks and should have used a set instead.

Common Pitfalls

  • Using a list when uniqueness matters and then manually removing duplicates later.
  • Using a set when you really need stable positional ordering.
  • Using a dictionary just to store unlabeled items that would be clearer in a list.
  • Forgetting that sets require hashable elements.
  • Choosing the structure you remember first instead of the one that matches the access pattern.

Summary

  • Use a list for ordered sequences with possible duplicates.
  • Use a dict for key-to-value relationships.
  • Use a set for uniqueness and fast membership testing.
  • Pick the structure based on the operations your code needs most often.
  • Clearer data-structure choices usually improve both readability and performance.

Course illustration
Course illustration

All Rights Reserved.