Last Updated : 12 Jun, 2025
Try it on GfG Practice
Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable.
Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier to find values.
Python
d = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print(d)
{1: 'Geeks', 2: 'For', 3: 'Geeks'}How to Create a Dictionary
Dictionary can be created by placing a sequence of elements within curly {} braces, separated by a 'comma'.
Python
d1 = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print(d1)
# create dictionary using dict() constructor
d2 = dict(a = "Geeks", b = "for", c = "Geeks")
print(d2)
{1: 'Geeks', 2: 'For', 3: 'Geeks'} {'a': 'Geeks', 'b': 'for', 'c': 'Geeks'}
Accessing Dictionary ItemsFrom Python 3.7 Version onward, Python dictionary are Ordered.
We can access a value from a dictionary by using the key within square brackets or get() method.
Python
d = { "name": "Prajjwal", 1: "Python", (1, 2): [1,2,4] }
# Access using key
print(d["name"])
# Access using get()
print(d.get("name"))
Adding and Updating Dictionary Items
We can add new key-value pairs or update existing keys by using assignment.
Python
d = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
# Adding a new key-value pair
d["age"] = 22
# Updating an existing value
d[1] = "Python dict"
print(d)
{1: 'Python dict', 2: 'For', 3: 'Geeks', 'age': 22}Removing Dictionary Items
We can remove items from dictionary using the following methods:
d = {1: 'Geeks', 2: 'For', 3: 'Geeks', 'age':22}
# Using del to remove an item
del d["age"]
print(d)
# Using pop() to remove an item and return the value
val = d.pop(1)
print(val)
# Using popitem to removes and returns
# the last key-value pair.
key, val = d.popitem()
print(f"Key: {key}, Value: {val}")
# Clear all items from the dictionary
d.clear()
print(d)
{1: 'Geeks', 2: 'For', 3: 'Geeks'} Geeks Key: 3, Value: Geeks {}Iterating Through a Dictionary
We can iterate over keys [using keys() method] , values [using values() method] or both [using item() method] with a for loop.
Python
d = {1: 'Geeks', 2: 'For', 'age':22}
# Iterate over keys
for key in d:
print(key)
# Iterate over values
for value in d.values():
print(value)
# Iterate over key-value pairs
for key, value in d.items():
print(f"{key}: {value}")
1 2 age Geeks For 22 1: Geeks 2: For age: 22
Nested DictionariesRead in detail: Ways to Iterating Over a Dictionary
Example of Nested Dictionary:
Python
d = {1: 'Geeks', 2: 'For',
3: {'A': 'Welcome', 'B': 'To', 'C': 'Geeks'}}
print(d)
{1: 'Geeks', 2: 'For', 3: {'A': 'Welcome', 'B': 'To', 'C': 'Geeks'}}
Python Dictionary Operation ProgramsRead in Detail: Python Nested Dictionary
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4