hackquest logo
P

Pavandeep

Harihar

1

Article

1304

View

0

Followers

0
0

Lists vs. Dictionaries in Python

Pavandeep
2025-10-26 08:39
0
0
Lists vs. Dictionaries in Python

Choosing the Right Data Structure for Performance Efficiency
In every programming language, choosing the right data structure can make or break performance. In Python, the most common choices for storing collections are lists and dictionaries — and while they may look similar, their internal workings and performance profiles differ dramatically.

Understanding when to use one over the other can help you write code that’s both faster and more memory-efficient — much like how Solidity developers pick between arrays and mappings for gas efficiency on Ethereum.

A Quick Refresher

  • Lists store ordered sequences of elements.
    You can access elements by index, iterate, append, and slice easily.

  • Dictionaries (or dicts) store key–value pairs.
    They provide constant-time lookups and updates but don’t maintain numeric ordering (before Python 3.7).


Example: Comparing Lists and Dictionaries

Let’s compare how Python handles the same data — user balances.
Using a List of Tuples


users = [("alice", 100), ("bob", 200), ("carol", 300)]

def get_balance_list(users, name):
    for user, balance in users:
        if user == name:
            return balance
    return 0

print(get_balance_list(users, "bob"))  # Output: 200
  • ✅ Simple and ordered

  • ❌ Inefficient lookups → O(n) time complexity

Using a Dictionary

# Using a dictionary
balances = {"alice": 100, "bob": 200, "carol": 300}

def get_balance_dict(balances, name):
    return balances.get(name, 0)

print(get_balance_dict(balances, "bob"))  # Output: 200
  • ✅ Constant-time lookup → O(1) on average

  • ❌ Cannot easily preserve order before Python 3.7

Performance Benchmark

Let’s measure the difference in lookup time for large datasets.

import time

# Create large datasets
users = [(f"user{i}", i) for i in range(1000000)]
balances = {f"user{i}": i for i in range(1000000)}

# Measure lookup time
start = time.time()
get_balance_list(users, "user999999")
print("List lookup:", time.time() - start)

start = time.time()
get_balance_dict(balances, "user999999")
print("Dict lookup:", time.time() - start)

Output :

List lookup: 0.12 seconds
Dict lookup: 0.00001 seconds

That’s nearly 10,000× faster using a dictionary right… ?


When to Use Which ?

Use Case

Recommended Structure

Reason

Fast lookup or membership checks

dict

O(1) average access time

Maintaining order or duplicates

list

Ordered and iterable

Both iteration + lookup

dict + list hybrid

Maintain order while keeping fast lookups

Example: Hybrid Pattern

balances = {}
user_list = []

def deposit(user, amount):
    if user not in balances:
        user_list.append(user)
    balances[user] = balances.get(user, 0) + amount

def get_total_balance():
    return sum(balances[u] for u in user_list)

This pattern mirrors the Solidity hybrid (mapping + array) approach:
constant-time lookups with the ability to iterate when needed.


Key Points

  • dictionary → Faster lookups, ideal for key-based access

  • lists → Best for ordered sequences or duplicates

  • Hybrid → Combine both for iteration + fast access

  • Rule of thumb: If you’re searching by key or ID, use a dictionary.


🔗 References

  • Python Docs: Data Structures — Lists, Tuples, Dictionaries

  • Time Complexity of Python Data Structures (Big O Cheat Sheet)

  • Real Python: Dictionaries vs Lists — When to Use Which

Original
Ecosystem:Gaia
Topic:DApp
Tag:
Python
Performance
DataStructures
Update at2025-10-26 08:40
0 / 1000