GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python Dictionaries (with Examples) - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python Dictionary

Last Updated : 19 Sep, 2026

Dictionary is a data structure that stores information in key-value pairs. While keys must be unique and immutable (like strings or numbers), values can be of any data type, whether mutable or immutable. This makes dictionaries ideal for accessing data by a specific name rather than a numeric position like in list.

Example: This example shows how a dictionary stores data using keys and values.

Python
data = { "name": "Jake", "age": 22 }
print(data)

Output
{'name': 'Jake', 'age': 22}

Explanation: "name" and "age" are keys, "Jake" and 22 are their values and dictionary stores data in key:value format

Creating a Dictionary

A dictionary is created by writing key-value pairs inside { }, where each key is connected to a value using colon (:). A dictionary can also be created using dict() function.

Python
a = {"x": 1, "y": 2}
print(a)

b = dict(name="Sam", age=20)
print(b)

Output
{'x': 1, 'y': 2}
{'name': 'Sam', 'age': 20}

Accessing Dictionary Items

A value in a dictionary is accessed by using its key. This can be done either with square brackets [ ] or with the get() method. Both return the value linked to the given key.

Python
d = {"name": "Kat", "age": 21}

print(d["name"])     # Access using key
print(d.get("age"))  # Access using get()

Output
Kat
21

Note: Accessing a missing key with [ ] raises a KeyError, while get() is safer because it returns None (or a default value) instead of an error.

Adding and Updating Dictionary Items

New items are added to a dictionary using the assignment operator (=) by giving a new key a value. If an existing key is used with the assignment operator, its value is updated with the new one.

Python
d = {"name": "Sam"}

d["age"] = 21        # Adding a new key-value pair
d["name"] = "Alex"   # Updating an existing value
print(d)

Output
{'name': 'Alex', 'age': 21}

Removing Dictionary Items

Dictionary items can be removed using built-in deletion methods that work on keys:

1. del: del removes an item using its key

Python
d = {"a": 1, "b": 2}
del d["a"]
print(d)

Output
{'b': 2}

2. pop(): pop() removes the item with the given key and returns its value

Python
d = {"a": 1, "b": 2}

val = d.pop("a")
print(val)
print(d)

Output
1
{'b': 2}

3. popitem(): popitem() removes and returns the last inserted key-value pair

Python
d = {"a": 1, "b": 2}
print(d.popitem())

Output
('b', 2)

4. clear(): clear() removes all items from the dictionary

Python
d = {"a": 1, "b": 2}
d.clear()
print(d)

Output
{}

Iterating Through a Dictionary

A dictionary can be traversed using a for loop to access its keys, values or both key-value pairs by using the built-in methods keys(), values() and items().

1. Iterate keys: Returns all keys from the dictionary.

Python
d = {"a": 1, "b": 2}
for key in d:
    print(key)

Output
a
b

2. Iterate values: Returns all values from the dictionary.

Python
d = {"a": 1, "b": 2}
for value in d.values():
    print(value)

Output
1
2

3. Iterate key-value pairs: Returns all key-value pairs as tuples.

Python
d = {"a": 1, "b": 2}
for key, value in d.items():
    print(key, value)

Output
a 1
b 2

Read in detail: Ways to Iterate Over a Dictionary

Nested Dictionaries

A nested dictionary is a dictionary that contains another dictionary as one of its values. Below diagram shows how a nested dictionary works, where key 3 points to another dictionary inside the main dictionary.

keys
Representation of Nested Dictionary

The arrows show how each key is connected to its corresponding value.

Python
d = {
    "student": {
        "name": "Sam",
        "age": 20
    }
}

print(d["student"]["name"])

Output
Sam
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.