Fundamentals 5 min read

Master Python Dictionaries and Sets: Tips, Tricks, and Common Pitfalls

This tutorial walks you through Python's dictionary and set data structures, covering creation, key uniqueness, updating, deleting, copying, and built‑in methods like keys(), while contrasting them with lists and tuples for clear, practical understanding.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Dictionaries and Sets: Tips, Tricks, and Common Pitfalls

Just as we look up words in a physical dictionary, Python's dict stores data as key‑value pairs. Creating a dictionary is straightforward:

dict_1 = {"key1": 100, "key2": 200, "key3": 300}
print(dict_1)  # {'key1': 100, 'key2': 200, 'key3': 300}
print(dict_1["key2"])  # 200

Keys must be unique; if a duplicate key appears, the later value overwrites the earlier one:

dict_2 = {"key1": 100, "key2": 200, "key3": 300, "key2": 400}
print(dict_2)  # {'key1': 100, 'key2': 400, 'key3': 300}

You can modify a value by assigning to its key:

dict_2["key3"] = 500
print(dict_2)  # {'key1': 100, 'key2': 400, 'key3': 500}

Removing a key uses the del statement:

del dict_2["key1"]
print(dict_2)  # {'key2': 400, 'key3': 500}

Assigning one dictionary to another (e.g., dict_3 = dict_2) copies the reference, so changes to one affect the other:

dict_3 = dict_2
dict_2["key2"] = 600
print(dict_3)  # {'key2': 600, ...}

To create an independent copy, use the copy() method:

dict_3 = dict_2.copy()
dict_2["key2"] = 600
print(dict_2)  # {'key2': 600, ...}
print(dict_3)  # original values remain unchanged

The keys() method returns a view of all keys, which can be converted to a list:

print(type(dict_2.keys()))  # <class 'dict_keys'>
print(list(dict_2.keys()))   # ['key1', 'key2', 'key3']

Python also provides the set type, defined with curly braces; duplicate elements are automatically removed:

set_1 = {"apple", "orange", "melon", "orange"}
print(set_1)  # {'apple', 'orange', 'melon'}

Recall the syntax differences: lists use [], tuples use (), and sets use {}. This foundation prepares you for more advanced topics such as conditional statements.

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.

PythonTutorialdictionarycopySet
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.