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…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Introduces 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

  1. Exercise 1: Create a dictionary to store information about a book. Include the title, author, year of publication, and number of pages.

  2. 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.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

JWT Decoder

Decode and validate JSON Web Tokens (JWT).

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

Markdown to HTML Converter

Convert Markdown to clean HTML.

Use tool

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help