Python / Python Data Structures
Working with Dictionaries and Sets
In this tutorial, you will learn about Python's powerful dictionary and set data structures. We will explore the key features of these structures and show you how to use them effe…
Section overview
5 resourcesIntroduces built-in data structures such as lists, tuples, dictionaries, and sets.
1. Introduction
This tutorial is designed to introduce you to Python's powerful dictionary and set data structures. By the end of this tutorial, you will understand how to create, manipulate, and use dictionaries and sets in Python.
You will learn:
- What dictionaries and sets are.
- How to create, access, update, and delete elements from these data structures.
- The differences and similarities between dictionaries, sets and other Python data structures.
Prerequisites: Basic understanding of Python programming (variables, functions, loops).
2. Step-by-Step Guide
Dictionaries
A dictionary is a mutable data type that stores mappings of unique keys to values. Here's how you can create a dictionary:
my_dict = {"key1": "value1", "key2": "value2", "key3": "value3"}
Sets
A set is an unordered collection of unique elements. Here's how to create a set:
my_set = {1, 2, 3, 4, 5}
3. Code Examples
Working with Dictionaries
# Creating a dictionary
person = {"name": "John", "age": 30, "city": "New York"}
# Accessing a value
print(person["name"]) # Output: John
# Updating a value
person["age"] = 31
print(person) # Output: {'name': 'John', 'age': 31, 'city': 'New York'}
# Adding a new key-value pair
person["profession"] = "Engineer"
print(person) # Output: {'name': 'John', 'age': 31, 'city': 'New York', 'profession': 'Engineer'}
# Deleting a key-value pair
del person["city"]
print(person) # Output: {'name': 'John', 'age': 31, 'profession': 'Engineer'}
Working with Sets
# Creating a set
numbers = {1, 2, 3, 4, 5}
# Adding an element
numbers.add(6)
print(numbers) # Output: {1, 2, 3, 4, 5, 6}
# Removing an element
numbers.remove(1)
print(numbers) # Output: {2, 3, 4, 5, 6}
# Checking if an element exists
print(3 in numbers) # Output: True
4. Summary
In this tutorial, you've learned how to work with dictionaries and sets in Python, including how to create, access, update, and delete elements. Both are powerful data structures that can make your programs more efficient and easier to understand.
Next steps for learning include working with nested dictionaries and sets, as well as exploring other built-in methods available for these data structures.
5. Practice Exercises
-
Exercise 1: Create a dictionary to store information about a book. Include the title, author, year of publication, and number of pages.
-
Exercise 2: Given a list of numbers, create a set to find and print the unique numbers in the list.
Solutions:
# Exercise 1
book = {
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"year": 1960,
"pages": 281
}
print(book)
# Exercise 2
numbers = [1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9]
unique_numbers = set(numbers)
print(unique_numbers) # Output: {1, 2, 3, 4, 5, 6, 7, 8, 9}
For further practice, try creating more complex dictionaries and sets, and practice manipulating them with the techniques you've learned in this tutorial.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article