Ojasa Mirai

Ojasa Mirai

Python

Loading...

Learning Level

🟒 BeginnerπŸ”΅ Advanced
🎯 Why Data Structures MatterπŸ“‹ Lists: Ordered CollectionsπŸ”‘ Dictionaries: Key-Value PairsπŸŽͺ Sets: Unique Values & SpeedπŸ“¦ Tuples: Immutable Sequencesβš–οΈ Comparing All Data StructuresπŸ”§ Mastering List Methods⚑ Advanced Dictionary PatternsπŸ”„ Power of Set OperationsπŸ—οΈ Building Nested Structuresβš™οΈ Performance Tuning & Optimization
Python/Data Structures/Dictionary Basics

πŸ” Advanced Dictionary Implementation β€” Hashing and Performance

Master the hash table internals that make dictionaries incredibly fast.


🎯 Hash Function and Key Distribution

Python dictionaries use hash functions to map keys to array indices. A good hash function distributes keys uniformly.

# Hash values for various types
print(hash("hello"))      # -9223372036854775208
print(hash(42))           # 42
print(hash((1, 2, 3)))    # 2528714134463
print(hash([1, 2, 3]))    # TypeError: unhashable type: 'list'

# Hash determinism (same object = same hash in same session)
s = "hello"
print(hash(s))            # Same value in this session
print(hash(s))            # Same value

# Hash randomization (different sessions)
# Running Python again: different hash value for security

# Custom hash function example
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __hash__(self):
        return hash((self.name, self.age))

    def __eq__(self, other):
        return self.name == other.name and self.age == other.age

people = {}
alice = Person("Alice", 30)
people[alice] = "Developer"

πŸ’‘ Hash Collision and Resolution

When multiple keys hash to the same index, Python uses open addressing to find the next available slot.

# Creating collisions is difficult due to randomization, but:

# Dictionary internal structure
d = {'a': 1, 'b': 2, 'c': 3}

# CPython stores in a hash table:
# Index: 0    1    2    3    4    5    6    7
# Entry: [a:1][--][b:2][--][c:3][--][--][--]

# With collision (hypothetically):
# hash('a') % 8 = index 0
# hash('x') % 8 = index 0 (collision!)
# Probing: tries index 1, 2, 3... until finding empty slot

# Load factor affects performance
# CPython resizes table when load factor > 2/3
# Triggers rehashing of all existing keys

🎨 Dictionary Growth and Rehashing

import sys

# Monitor dictionary size growth
d = {}
for i in range(10):
    d[i] = i
    print(f"Keys: {len(d)}, Memory: {sys.getsizeof(d)} bytes")

# Output shows table resizing:
# Keys: 1, Memory: 240 bytes
# Keys: 2, Memory: 240 bytes
# Keys: 3, Memory: 240 bytes
# ...
# Keys: 6, Memory: 240 bytes
# Keys: 7, Memory: 368 bytes  <- Resized!
# Keys: 8, Memory: 368 bytes

# Rehashing cost
large_dict = {i: i for i in range(1000)}
# All 1000 keys rehashed when growing

πŸ“Š Dictionary Performance Profile

OperationComplexityNotes
`d[key]` lookupO(1) averageO(n) worst case (rare)
`d[key] = value`O(1) averageMay trigger resize
`del d[key]`O(1) averageMarks slot as deleted
`key in d`O(1) averageSame as lookup
`.keys()`O(1)Returns view (lazy)
`.values()`O(1)Returns view (lazy)
`.items()`O(1)Returns view (lazy)
`.get(key, default)`O(1) averageSafe access
`.pop(key, default)`O(1) averageRemove & return
`.update()`O(n)n = size of update dict

🎯 Dictionary Views and Iteration

# Views are dynamicβ€”reflect changes
d = {'a': 1, 'b': 2}
keys_view = d.keys()

print(list(keys_view))  # ['a', 'b']

d['c'] = 3
print(list(keys_view))  # ['a', 'b', 'c'] - updated!

# Memory efficient iteration
for key in d:           # Iterates through keys
    print(key)

for key in d.keys():    # Same as above
    print(key)

for value in d.values():
    print(value)

for key, value in d.items():
    print(key, value)

# Iteration order (Python 3.7+)
# Dictionaries maintain insertion order
d = {}
d['z'] = 26
d['a'] = 1
d['m'] = 13
list(d.keys())  # ['z', 'a', 'm'] - insertion order preserved!

πŸ”‘ Key Takeaways

  • βœ… Dictionaries use hash tables for O(1) average lookup
  • βœ… Hash function maps keys to indices uniformly
  • βœ… Open addressing resolves collisions
  • βœ… Resizing triggers when load factor exceeds 2/3
  • βœ… Dictionary views are dynamic and memory efficient
  • βœ… Insertion order preserved (Python 3.7+)

Ready to practice? Challenges | Quiz


Resources

Python Docs

Ojasa Mirai

Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.

Learn Deep β€’ Build Real β€’ Verify Skills β€’ Launch Forward

Courses

PythonFastapiReactJSCloud

Β© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn