Python Foundation

Dictionaries in Python

01 · What is a Dictionary? Dictionaries · 1 of 9

What is a Dictionary?

A dictionary is a collection of key-value pairs.

Each key is unique and is used to access its corresponding value.

Python
student = {
    "name": "Kundan",
    "age": 25,
    "course": "Data Science"
}

print(student)
print(type(student))
Output
{'name': 'Kundan', 'age': 25, 'course': 'Data Science'}
<class 'dict'>

Key Characteristics

  • Stores data in key : value format
  • Keys must be unique and immutable (string, number, tuple)
  • Values can be of any data type
  • Ordered (from Python 3.7+)
  • Mutable
02 · Creating Dictionaries Dictionaries · 2 of 9

Creating Dictionaries

Using Curly Braces

Python
person = {
    "name": "Ravi",
    "age": 28,
    "city": "Hyderabad"
}
print(person)

Using dict() Constructor

Python
person2 = dict(name="Priya", age=24, city="Mumbai")
print(person2)

empty_dict = {}
print(type(empty_dict))
Output
{'name': 'Priya', 'age': 24, 'city': 'Mumbai'}
<class 'dict'>
03 · Accessing Items Dictionaries · 3 of 9

Accessing Dictionary Items

Python
student = {
    "name": "Kundan",
    "age": 25,
    "course": "Data Science",
    "city": "Hyderabad"
}

print(student["name"])
print(student.get("age"))
print(student.get("grade", "Not Available"))  # Default value
Output
Kundan
25
Not Available

Using .get() is safer because it does not raise an error if the key is missing.

04 · Adding & Updating Dictionaries · 4 of 9

Adding and Updating Items

Python
student = {
    "name": "Kundan",
    "age": 25
}

# Adding new key-value
student["course"] = "Data Science"
print(student)

# Updating existing value
student["age"] = 26
print(student)

# Using update()
student.update({"city": "Hyderabad", "age": 27})
print(student)
Output
{'name': 'Kundan', 'age': 25, 'course': 'Data Science'}
{'name': 'Kundan', 'age': 26, 'course': 'Data Science'}
{'name': 'Kundan', 'age': 27, 'course': 'Data Science', 'city': 'Hyderabad'}
05 · Removing Items Dictionaries · 5 of 9

Removing Items from Dictionary

Python
student = {
    "name": "Kundan",
    "age": 25,
    "course": "Data Science",
    "city": "Hyderabad"
}

student.pop("age")           # Remove specific key
print(student)

student.popitem()           # Remove last inserted item
print(student)

del student["course"]
print(student)

student.clear()             # Remove all items
print(student)
Output
{'name': 'Kundan', 'course': 'Data Science', 'city': 'Hyderabad'}
{'name': 'Kundan', 'course': 'Data Science'}
{'name': 'Kundan'}
{}
06 · Dictionary Methods Dictionaries · 6 of 9

Important Dictionary Methods

Python
student = {
    "name": "Kundan",
    "age": 25,
    "course": "Data Science"
}

print(student.keys())      # All keys
print(student.values())    # All values
print(student.items())     # All key-value pairs

print("name" in student)
print(len(student))
Output
dict_keys(['name', 'age', 'course'])
dict_values(['Kundan', 25, 'Data Science'])
dict_items([('name', 'Kundan'), ('age', 25), ('course', 'Data Science')])
True
3
07 · Looping Through Dict Dictionaries · 7 of 9

Looping Through a Dictionary

Python
student = {
    "name": "Kundan",
    "age": 25,
    "course": "Data Science"
}

# Loop through keys
for key in student:
    print(key)

print("-----")

# Loop through keys and values
for key, value in student.items():
    print(key, ":", value)
Output
name
age
course
-----
name : Kundan
age : 25
course : Data Science
08 · Nested Dictionaries Dictionaries · 8 of 9

Nested Dictionaries

A dictionary can contain another dictionary as its value.

Python
students = {
    "student1": {
        "name": "Kundan",
        "age": 25
    },
    "student2": {
        "name": "Priya",
        "age": 24
    }
}

print(students["student1"]["name"])
print(students["student2"]["age"])
Output
Kundan
24
09 · Summary Dictionaries · 9 of 9

Summary – Dictionaries in Python

  • Dictionary stores data as key-value pairs.
  • Created using { } or dict().
  • Keys must be unique and immutable.
  • Access values using dict[key] or dict.get(key).
  • Important methods: keys(), values(), items(), update(), pop(), clear()
  • Dictionaries are widely used for structured data.

You have completed the core Python data structures!

Home Dictionaries in Python

Dictionaries in Python

Learn how to store and manage data using key-value pairs.

Select any topic from the left sidebar to begin.


Start Learning →