Skip to content

How to Choose Between Lists and Dictionaries in Python

Compare Python lists (ordered sequences accessed by index) and dictionaries (key-value pairs with O(1) lookups) to select the right data structure for performance and clarity in your code.

Lists and dictionaries serve fundamentally different purposes. Lists store ordered sequences accessed by position, while dictionaries store key-value pairs accessed by meaningful identifiers. Choosing correctly impacts both code clarity and performance.

Core Differences

Feature List Dictionary
Access Method By index [0] By key ["name"]
Lookup Speed O(n) scan O(1) hash
Order Preserved ✅ Always ✅ Python 3.7+
Duplicates ✅ Allowed ❌ Keys unique

When to Use Lists

Lists excel when position matters or you're working with sequential data:

steps = ["Download", "Install", "Configure", "Run"]
first_step = steps[0]  # "Download"

temperatures = [72, 75, 68, 71, 73]
average = sum(temperatures) / len(temperatures)

queue = ["Alice", "Bob", "Charlie"]
next_person = queue.pop(0)

When to Use Dictionaries

Dictionaries shine when you need fast lookups by meaningful keys:

user = {"name": "Alice", "email": "alice@example.com", "age": 30}
print(user["email"])  # Instant access

config = {"debug": True, "max_connections": 100, "timeout": 30}
if config["debug"]:
    enable_logging()

word_count = {}
for word in text.split():
    word_count[word] = word_count.get(word, 0) + 1

The Lookup Performance Gap

This difference becomes critical with large datasets:

users_list = ["alice", "bob", "charlie", ...]  # 10,000 users
if "zachary" in users_list:  # Scans up to 10,000 items
    print("Found")

users_dict = {"alice": True, "bob": True, ...}  # 10,000 users
if "zachary" in users_dict:  # Single hash lookup
    print("Found")

Common Patterns

List of Dictionaries

Combine both for tabular data:

employees = [
    {"id": 1, "name": "Alice", "role": "Engineer"},
    {"id": 2, "name": "Bob", "role": "Designer"},
    {"id": 3, "name": "Charlie", "role": "Manager"}
]

first_employee_name = employees[0]["name"]

engineers = [e for e in employees if e["role"] == "Engineer"]

Dictionary for Fast Lookups

Convert list to dictionary when you need repeated access by a specific field:

users = [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]
def find_user(user_id):
    for user in users:
        if user["id"] == user_id:
            return user

users_by_id = {user["id"]: user for user in users}
alice = users_by_id[1]  # Instant

Decision Guide

Scenario Best Choice
Sequential data List
Accessing by position List
Need fast lookups by identifier Dictionary
Named/labeled data Dictionary
Counting or grouping Dictionary
Maintaining insertion order only List

Summary

  • Lists: Ordered sequences accessed by numeric index
  • Dictionaries: Key-value mappings with O(1) lookups

Choose lists when position matters. Choose dictionaries when you need fast access by meaningful keys. For large collections with frequent membership testing, the performance difference is substantial.

Navigation