codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

21 / 68 topics
19Python Lists20Python Tuples21Python Dictionaries22Python Sets23Python Arrays
Tutorials/Python Programming/Python Dictionaries
🐍Python Programming

Python Dictionaries

Updated 2026-05-15
30 min read

Python Dictionaries

Dictionaries are a fundamental data structure in Python that allow you to store and manage key-value pairs. They are highly efficient for retrieving data, making them indispensable for various applications such as configuration settings, caches, and more. In this tutorial, we'll explore how to create dictionaries, access their values, modify them, and use some of the built-in methods they provide.

Introduction

Dictionaries in Python are similar to associative arrays or hash tables in other programming languages. They consist of a collection of key-value pairs where each key is unique. Dictionaries are mutable, meaning you can add, update, or remove items after their creation. This flexibility makes them versatile and powerful for various data manipulation tasks.

Creating Dictionaries

You can create a dictionary using curly braces {} with key-value pairs separated by colons :. Alternatively, you can use the dict() constructor.

Using Curly Braces

Python
1# Creating a dictionary using curly braces
2person = {
3 "name": "Alice",
4 "age": 30,
5 "city": "New York"
6}
Output

Accessing Values

You can access values in a dictionary using their corresponding keys. If you try to access a key that doesn't exist, Python will raise a KeyError.

Python
1# Accessing values by key
2name = person["name"]
3age = person.get("age")
Output

Adding/Updating Keys

You can add new key-value pairs to a dictionary or update existing ones by assigning values to keys.

Python
1# Adding a new key-value pair
2person["occupation"] = "Engineer"
3
4# Updating an existing key
5person["age"] = 31
Output

Using pop() Method

The pop() method removes the key and returns its value. If the key doesn't exist, it raises a KeyError, unless you provide a default value.

Python
1# Removing a key-value pair using pop()
2age = person.pop("age")
Output

setdefault()

The setdefault() method returns the value of a key if it exists in the dictionary. If not, it inserts the key with a specified default value and returns that value.

Python
1# Using setdefault()
2person.setdefault("city", "Los Angeles")
Output

Dictionary Comprehensions

Dictionary comprehensions provide a concise way to create dictionaries. They are similar to list comprehensions but use curly braces {}.

Python
1# Creating a dictionary using a comprehension
2squares = {x: x**2 for x in range(5)}
Output

Summary

  • Creating Dictionaries: Use curly braces {} or dict().
  • Accessing Values: Use square brackets [] or the get() method.
  • Adding/Updating Keys: Assign values to keys directly.
  • Removing Keys: Use del statement or pop() method.
  • Dictionary Methods: Include keys(), values(), items(), setdefault(), and update().
  • Dictionary Comprehensions: Provide a concise way to create dictionaries.

What's Next?

In the next tutorial, we'll explore Python sets, another essential data structure. Sets are unordered collections of unique elements that are useful for membership testing, removing duplicates from sequences, and performing mathematical set operations like unions, intersections, and differences. Stay tuned!


PreviousPython TuplesNext Python Sets

Recommended Gear

Python TuplesPython Sets