Fundamentals 9 min read

Unlock Python’s Speed: Master dict and set for Fast Data Lookup

This article explains Python’s built-in dict and set structures, comparing them with lists, demonstrating their fast key-value lookups, handling missing keys, and common operations like insertion, deletion, and set algebra, while providing clear code examples and practical tips for effective use.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Unlock Python’s Speed: Master dict and set for Fast Data Lookup

1. dict

Python built-in dictionary (dict) supports key-value storage, also known as map in other languages, offering extremely fast lookup.

Example: suppose we need to find a student's score by name. Using lists requires two parallel lists.

names = ['Michael', 'Bob', 'Tracy']</code><code>scores = [95, 75, 85]

To find a score, you must locate the index in names then fetch from scores, which becomes slower as the lists grow.

Using a dict you need only one mapping from name to score; lookup time remains constant regardless of size.

d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}</code><code>print(d['Michael'])

1. Why is dict lookup so fast?

Dict uses a hash table that maps a key directly to a bucket, similar to looking up a word in a dictionary index, so the time does not grow with size.

You can add entries after creation:

d['Adam'] = 67</code><code>print(d['Adam'])

Assigning a new value to an existing key overwrites the previous one:

d['Jack'] = 90</code><code>d['Jack'] = 88</code><code>print(d['Jack'])  # shows 88

Accessing a non‑existent key raises a KeyError:

print(d['Thomas'])

2. Avoid missing-key errors

2.1 Use in to test key existence

print('Thomas' in d)

2.2 Use dict.get() with default

d.get('Thomas')</code><code>print(d.get('Thomas', -1))

Note: When get() returns None, the interactive interpreter shows no output.

To delete a key use pop(key) which also removes its value:

d.pop('Bob')</code><code>print(d)

3. Comparison with list

Lookup and insertion are extremely fast and do not depend on the number of keys.

Dict consumes more memory; lists use less.

In lists, lookup and insertion time grows with size.

Dict trades space for time.

2. set

A set is similar to a dict but stores only unique keys without associated values.

Create a set from a list:

s = set([1, 2, 3])</code><code>print(s)

Duplicate elements are automatically filtered:

s = set([1, 1, 2, 2, 3, 3])</code><code>print(s)

Add elements with add() (adding an existing element has no effect):

s.add(4)</code><code># s becomes {1, 2, 3, 4}

Remove elements with remove():

s.remove(2)</code><code>print(s)

Sets support mathematical operations such as intersection and union:

s1 = set([1, 2, 3])</code><code>s2 = set([2, 3, 4])</code><code>print(s1 & s2)</code><code>print(s1 | s2)

Difference between set and dict

Sets store only keys, not values, and like dicts they cannot contain mutable objects because equality cannot be determined.

3. Immutable vs mutable objects

Strings are immutable, lists are mutable. Modifying a mutable object changes it in place, while operations on immutable objects return new objects.

Example with list sorting:

a = ['c', 'b', 'a']</code><code>a.sort()  # a becomes ['a', 'b', 'c']</code><code>print(a)

Example with string replacement:

a = 'abc'</code><code>b = a.replace('a', 'A')</code><code>print(b)</code><code>print(a)

Note: Methods on immutable objects never modify the original; they produce a new object.

4. Summary

This article introduced Python’s dict and set structures, highlighting the importance of using immutable objects as keys and demonstrating common operations with clear code examples.

Through step-by-step explanations and practical cases, readers can better understand and apply these fundamental data structures.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonData Structureshash tableImmutable ObjectsSetdict
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.