Python Dictionary Master Guide | Beginners to Advanced in 2026

Python Dictionary Master Guide | Beginners to Advanced in 2026
Python Dictionary Master Guide | Beginners to Advanced in 2026

📑 Table of Contents

Dictionary Definition in Python

Dictionary: A dictionary is an unordered collection of data of different data types. Dictionary data can be changed. A dictionary is created using curly brackets. Dictionary items are in the form of key-value pairs.

A dictionary in python is an unordered mutable and indexed collection of data that stores key-value pairs. Each key in a dictionary must be unique and immutable (like List, a string, number, or tuple) while the values can be of any data type including lists, dictionaries. Dictionaries are enclosed in curly braces {} with each key separated from its value by a colon (:).

Syntax of Dictionary in Python

data={key1:value1,key2:value2,key3:value3.....keyn:valuen}

Dictionary Example Program in Python

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print(data)
print(type(data))
============OUTPUT============
{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python'}

<class 'dict'>


Dictionary Methods in Python

Len Function: It can be used to get the length numbers of elements in dictionary items.
Key Function: The keys() method in python is a built-in dictionary method used to return a view object containing all the keys of a dictionary.
Items Function: The items() method in python is a built-in dictionary method used to return a view object of all key-value pairs in a dictionary.
Values Function: The values() method in python is a built-in dictionary method used to return a view object containing all the values stored in a dictionary.
Get Function: The get() method in python is a built-in dictionary method used to retrieve the value of a specified key.
Update Function: The update() method in python is a built-in dictionary method used to add new key-value pairs to a dictionary or modify existing ones.
Pop Function: The pop() method in python is a built-in dictionary method used to remove a specified key from a dictionary and return its value.
Clear Function: The clear() method in python is a built-in dictionary method used to remove all items (key-value pairs) from a dictionary, leaving it empty.
Copy Function: The copy() method in python is a built-in dictionary method used to create a shallow copy of a dictionary.
Fromkeys Function: The fromkeys() method in python is a built-in dictionary method used to create a new dictionary from a sequence of keys.

Dictionary Key Function Python

Key: We can access items of dictionary using key method. Key is a unique identifier used to store the value and access data.

Example of Dictionary Key in Python

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print("Student Name:",data["name"])
============OUTPUT============
Student Name: Jiocoding


Printing key and value. Access values of dictionary using loop printing key.

How to Print Key using For Loop

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
for key in data:
    print(key)
============OUTPUT============
name
age
City
Language

How to print key value pair using Loop

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
for key in data:
    print(key,"of Company Data:",data[key])
============OUTPUT============
name of Company Data: Jiocoding

age of Company Data: 33
City of Company Data: Noida
Language of Company Data: Python

Dictionary Get Function in Python

We can also access items of dictionary using get() function. The get() method in Python dictionaries returns the value for a given key and avoids raising an error if the key is missing by returning None or a specified default value instead.

Example of Dictionary in Python with Output

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print("Student Name:",data.get("name"))
============OUTPUT============
Student Name: Jiocoding


How to use update Function in Dictionary in Python

Update items of dictionary We can change the value of dictionary. The update() method in Python is a built-in dictionary method used to add new key-value pairs or modify existing ones using another dictionary or an iterable of key-value pairs.

Example of Update in Python

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print("Before Update Dictionary")
print(data)

data["City"]="Delhi"
print("After Update Dictionary")
print(data)
============OUTPUT============
Before Update Dictionary

{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python'}
After Update Dictionary
{'name': 'Jiocoding', 'age': 33, 'City': 'Delhi', 'Language': 'Python'}


Dictionary Length Python 

Length of dictionary: len() function is used to get length of dictionary. The length of a dictionary refers to the number of key-value pairs it contains. Python does not have a special length method for dictionaries. It is uses the built-in len() function to find the number of items in a dictionary.

Example of Dictionary in Python with Output

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print("Length of dictionary:",len(data))
============OUTPUT============
Length of dictionary: 4


Add New Dictionary Item Python

we can also add new item to a dictionary using key. A new item can be added to a dictionary by assigning a value to a new key using square bracket notation. If the key does not already exist, python creates a new key-value pair in the dictionary.

Example of Add new Dictionary Item

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print("Before Add New Dictionary Item")
print(data)
data["Courses"]="Python_Language"
print("After Before Add New Dictionary Item")
print(data)
============OUTPUT============
Before Add New Dictionary Item
{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python'}

After Before Add New Dictionary Item
{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python', 'Courses': 'Python_Language'}

Dictionary Pop Method in Python

The pop() method in Python is a built-in dictionary method used to remove a specified key and return its corresponding value. If the key is not found and no default value is provided raises a KeyError. Pop() function is used to remove specified item from a dictionary.

Example of Dictionary in Python with Output

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print("Before Delete Dictionary Items")
print(data)
data.pop("age")
print("After Delete Dictionary Items")
print(data)
============OUTPUT============
Before Delete Dictionary Items

{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python'}
After Delete Dictionary Items
{'name': 'Jiocoding', 'City': 'Noida', 'Language': 'Python'}

Del Method in Dictionary Python

del keyword is also used to remove specified item from a dictionary.

Example of Del Method Dictionary in Python

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print("Before using Del Dictionary Items")
print(data)
del data["age"]
print("After using Del Dictionary Items")
print(data)
============OUTPUT============
Before using Del Dictionary Items

{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python'}
After using Del Dictionary Items
{'name': 'Jiocoding', 'City': 'Noida', 'Language': 'Python'}

Delete dictionary: del keyword is also used to delete dictionary completely.

Example of Delete Dictionary in Python

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print("Before Delete Dictionary Items")
print(data)
del data

print("After Delete Dictionary Items")
print(data)

============OUTPUT============
Before Delete Dictionary Items
{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python'}
After Delete Dictionary Items
NameError: name 'data' is not defined


Dictionary Clear Method in Python

clear() function is used to clear or empty the dictionary. The clear() method in python is a built-in dictionary method used to remove all items (key-value pairs) from a dictionary, leaving it empty.

Example of Clear Function in Python

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print("Before using clear Dictionary Items")
print(data)
data.clear()
print("After using clear Dictionary Items")
print(data)

============OUTPUT============
Before using clear Dictionary Items
{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python'}
After using clear Dictionary Items
{}


Nested Dictionary in Python

A nested dictionary in Python is a dictionary inside another dictionary. It means one or more values in a dictionary are themselves dictionaries.

Nested Dictionary in Python Example

student_data={
    "Jiocoding":{
            "id":465473543,
             "Website Name":"Jiocoding",
             "City":"Noida"
            },
    "Ramesh":{
                    "id":4368644,
                     "percent":89.6,
                     "City":"Delhi"
                    }
        }

print("Jiocoding Data:",student_data["Jiocoding"])
print("Ramesh Data:",student_data["Ramesh"])
============OUTPUT============
Jiocoding Data: {'id': 465473543, 'Website Name': 'Jiocoding', 'City': 'Noida'}

Ramesh Data: {'id': 4368644, 'percent': 89.6, 'City': 'Delhi'}


Dictionary Copy Method in Python

The copy() method in python is a built-in dictionary method used to create a shallow copy of a dictionary. It returns a new dictionary with the same key-value pairs as the original dictionary.

Example of Dictionary Copy Method in Python

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print(data)
data1=data.copy()
print(data1)

============OUTPUT============
{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python'}
{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python'}

Dictionary Fromkeys Function in Python

This method creates a new dictionary with specified key and value. The fromkeys() method in Python is a built-in dictionary method used to create a new dictionary from a sequence of keys with all keys assigned the same specified value.

Example of Dictionary Fromkeys in Python

dataset=('data1','data2','data3')
dict_data1=dict.fromkeys(dataset)
print("Without Value:",dict_data1)
dict_data2=dict.fromkeys(dataset,100)
print("With Value:",dict_data2)

============OUTPUT============
Without Value: {'data1': None, 'data2': None, 'data3': None}

With Value: {'data1': 100, 'data2': 100, 'data3': 100}

Dictionary Item Method in Python

It returns all the key-value pair of the dictionary.

Example of Dictionary Items in Python

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print("Dictionary Key:",data.items())
============OUTPUT============
Dictionary Key: dict_items([('name', 'Jiocoding'), ('age', 33), ('City', 'Noida'), ('Language', 'Python')])


Set Default Method in Dictionary Python

This method is used to set a default value to a key. If the specified key is present in the dictionary then it returns its value.

Example of Dictionary Default Method 1 in Python

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
x=data.setdefault("name")
print(data)
print(x)

============OUTPUT============
{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python'}

Jiocoding


If the specified key is not present in the dictionary then it insert key into dictionary with default value 'None'. We can also set value of key using setdefault function.


Example of Dictionary Default Method 2 in Python

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print(data)
x=data.setdefault("Google")
print(data)
============OUTPUT============
{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python'}

{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python', 'Google': None}

Dictionary Values Method in Python

It returns all the values of the dictionary. A values() method in python is a built-in dictionary method used to return a view object containing all the values in a dictionary. The view object reflects changes made to the dictionary.

Example of Dictionary Values Method in Python

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print("Dictionary Data:",data.values())
============OUTPUT============
Dictionary Data: dict_values(['Jiocoding', 33, 'Noida', 'Python'])


Dictionary Update Method in Python

This method updates the dictionary with the key and value pairs. The update() method in python updates a dictionary by adding new key-value pairs or modifying existing ones. Existing keys are updated and new keys are added to the dictionary.

Example of Update Dictionary in Python

data={"name":"Jiocoding","age":33,"City":"Noida","Language":"Python"}
print("Before Update Dictionary Data")
print(data)
data.update({"pincode":435454})
print("After Update Dictionary Data")
============OUTPUT============
Before Update Dictionary Data
{'name': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python'}
After Update Dictionary Data
{'sid': 'Jiocoding', 'age': 33, 'City': 'Noida', 'Language': 'Python', 'pincode': 435454}


 

Post a Comment

0 Comments