• Fri, Jun 2026

Python Dictionaries and Sets Explained with Examples for Beginners

Python Dictionaries and Sets Explained with Examples for Beginners

Learn Python Dictionaries and Sets with definitions, examples, and practical uses. Understand how to store key-value pairs, manage unique data, and boost coding efficiency.

Python offers powerful built-in data structures that make coding faster, cleaner, and more efficient. Among these, Dictionaries and Sets stand out because they allow you to work with data in ways that lists and tuples cannot.

In this article, we’ll break down what Dictionaries and Sets are, how they work, and provide practical examples to help you use them effectively.

1. What is a Python Dictionary?

A Dictionary in Python is a collection of key-value pairs. Each key must be unique, while values can be duplicated. Think of it like a real-world dictionary where a word (key) maps to its meaning (value).

Syntax:

my_dict = {
    "name": "Alice",
    "age": 25,
    "city": "New York"
}

1.1 Accessing Dictionary Elements

print(my_dict["name"])   # Output: Alice
print(my_dict.get("age")) # Output: 25

1.2 Adding and Updating Items

my_dict["email"] = "alice@example.com"  # Add new key-value pair
my_dict["age"] = 26  # Update value

1.3 Removing Items

del my_dict["city"]       # Remove by key
removed = my_dict.pop("email")  # Remove and return value
my_dict.clear()           # Empty the dictionary

1.4 Dictionary Iteration

for key, value in my_dict.items():
    print(key, ":", value)

1.5 Practical Example

Imagine you are building a student record system:

student = {
    "id": 101,
    "name": "John",
    "marks": {"Math": 90, "Science": 85}
}

print(student["marks"]["Math"])  # Output: 90

✅ Actionable Tip: Use dictionaries when you need fast lookups or when data naturally maps from keys to values.

2. What is a Python Set?

A Set is an unordered collection of unique elements. It is useful when you want to avoid duplicates or perform mathematical operations like union, intersection, and difference.

Syntax:

my_set = {1, 2, 3, 4}

2.1 Adding and Removing Elements

my_set.add(5)     # Add element
my_set.remove(2)  # Remove element

2.2 Set Operations

A = {1, 2, 3}
B = {3, 4, 5}

print(A.union(B))        # {1, 2, 3, 4, 5}
print(A.intersection(B)) # {3}
print(A.difference(B))   # {1, 2}

2.3 Checking Membership

print(3 in A)  # True
print(6 in A)  # False

2.4 Practical Example

Removing duplicate values from a list:

numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = list(set(numbers))
print(unique_numbers)  # [1, 2, 3, 4, 5]


✅ Actionable Tip: Use sets when you need to ensure uniqueness or perform fast membership checks.


3. Differences Between Dictionaries and Sets

FeatureDictionarySet
Structure
 
Key-value pairs
Unique values only
DuplicatesKeys must be unique, values can repeatNo duplicates allowed
Data AccessAccessed via keys (my_dict[key])Checked via membership (in)
Use CasesStoring structured data (records)Ensuring uniqueness, set operations

4. When to Use Dictionaries vs Sets

✅ Use a Dictionary when you want to map relationships (e.g., user data, product catalog, settings).

✅ Use a Set when you want to eliminate duplicates or perform mathematical set operations.

Conclusion

Python Dictionaries and Sets are indispensable tools for developers. Dictionaries excel in managing structured key-value data, while Sets are perfect for unique collections and mathematical operations.

Mastering these two will not only make your code cleaner but also improve performance in real-world applications.

This website uses cookies to enhance your browsing experience. By continuing to use this site, you consent to the use of cookies. Please review our Privacy Policy for more information on how we handle your data. Cookie Policy