Skip to ContentGo to accessibility page

10.6 Chapter summary

Highlights from this chapter include:

  • A dictionary in Python is a container object including key-value pairs.
  • The dict type implements a dictionary in Python.
  • A dictionary cannot have duplicate keys.
  • A dictionary is a mutable object but keys in the dictionary must be immutable objects.
  • A dictionary can be created using curly braces or the dict() method.
  • Values in the dictionary can be obtained through square bracket notation or the get() method.
  • Dictionary items, keys, and values can be obtained using items(), keys(), and values() methods, respectively.
  • Existing items can be modified or new items can be added to a dictionary using square brackets notation or the update() method.
  • Items can be removed from a dictionary using the del keyword or the pop() method.
  • Conditional statements can be used with a dictionary to check if the dictionary contains specific keys, values, or key-value pairs.
  • Looping on a dictionary can be done by iterating over keys, values, or items.
  • Nested dictionaries are dictionaries that are stored as values within another dictionary.
  • With dictionary comprehension, elements of an iterable object are transformed into key-value pairs.

At this point, you should be able to use dictionaries in your programs. The programming practice below ties together most topics presented in the chapter.

Concept Description
Dictionary creation using curly braces
my_dict = {key1:value1, key2:value2}

Dictionary creation using the dict() method

# Using a list
my_list = [(key1, value1), (key2, value2)]
my_dict = dict(my_list)

# Using keyword arguments
my_dict = dict(key1=value1, key2=value2)

# From another dictionary
old_dict = {key1: value1, key2: value2}
new_dict = dict(old_dict)
Accessing dictionary items
my_dict = {key1: value1, key2: value2}

# Accessing item using square bracket notation
my_dict[key1]

# Accessing item through get() method
my_dict.get(key1)
Accessing all dictionary items
my_dict.items()
Accessing all dictionary keys
my_dict.keys()
Accessing all dictionary values
my_dict.values()
Adding a new key-value pair or updating an existing key-value pair
my_dict = {key1: value1, key2: value2}

# Updating an item using square bracket notation
my_dict[key1] = new_value
# Adding a new key-value pair using square bracket notation
my_dict[key3] = value3

# Updating an item using update() method
my_dict.update({key1: new_value})
# Adding a new key-value pair using update() method
my_dict.update({key3: value3})
Deleting a key-value pair from a dictionary
my_dict = {key1: value1, key2: value2}

# Using del keyword
del my_dict[key1]

# Using pop() method
deleted_value = my_dict.pop(key1)
Iterating over a dictionary
for key in dictionary: # Loop expression
  # Statements to execute in the loop

#Statements to execute after the loop
Nested dictionaries
{
key_1:{key11:value11, key12:value12},
key_2:{key21:value21, key22:value22}
}
Dictionary comprehension
{key_expression: value_expression for element in iterable}
Table 10.10 Chapter 10 reference.

Try It

Even and odd values

Given a list, create a dictionary with two keys, "even" and "odd". The values associated with each key must be the list of corresponding even and odd values in the given list.

    Input:
    input_list = [3, 5, 6, 1]
    Prints {"even": [6], "odd":[3, 5, 1]}
Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.