Working on a Python automation project for a retail analytics company in the USA, I had to manage large datasets stored as JSON files. These files contained customer details, sales numbers, and product metadata, all structured as Python dictionaries.
At that point, I realized how essential it is to master Python dict methods. These methods make it incredibly easy to add, update, remove, and manipulate data efficiently.
In this tutorial, I’ll walk you through 9 of the most useful Python dictionary methods that I use daily in my projects. I’ll also include complete Python code examples so you can try them out immediately.
How to Use Python Dict Methods
Let’s understand the scenario first; it is really important to use the correct method in the correct situation. Let’s take a scenario with an example,
emp_data = {"names": ["john", "jake", "peter", "robert"],
"age": [34,23,45,28],
"salary": [12000, 15000, 25000, 18000]}Suppose this is my employees’ data, and I want to make some changes to it, like adding a new key and incrementing the salary by 10%, etc.
In this situation, you can use some dictionary methods to target particular elements of the key, etc. Let’s understand all the Python dict methods individually with practical examples and using some realistic scenarios.
Method 1: Python Dictionary Update() Method
First, we will see the dictionary’s update() method in Python. The update method allows you to add new keys and values if the key does not exist, and if it does exist, it will modify its value.
Syntax
dict.update({key, value})- {key, value}: Here, if the given key already exists, then it will modify its value. Instead of giving a key-value pair, you can give the dictionary directly, so it will update it.
Let’s understand how the update() method will work in Python dict methods.
emp_data = {"names": ["john", "jake", "peter", "robert"],
"age": [34,23,45,28],
"salary": [12000, 15000, 25000, 18000]}
data = {"id":[1001,1002,1003,1004]}
data.update(emp_data)
print(data,"\n")
data.update({"present_all":True})
print(data)I executed the above example code and added the screenshot below.

We are using the update() method in both ways. We have a dictionary of emp_data, then got another dictionary where id was there and we need to add it with emp_data.
Method 2: Python Dict Method keys()
Now, we will explain the keys() method, a built-in method in Python. The keys() method is used to get the available keys in the dictionary, and it returns values like dict_keys([‘key1’, ‘key2’, ‘key3’]).
Syntax
dictionary_name.keys()- dictionary_name.keys(): This method is specially made for dict, so it will not work for other collections.
Let’s understand how the key() method will be an important part of the Python Dict methods.
employee_info = {
"John Doe": {"position": "Manager", "department": "Sales", "salary": 70000},
"Jane Smith": {"position": "Engineer", "department": "Engineering", "salary": 80000},
"Mike Johnson": {"position": "Analyst", "department": "Finance", "salary": 65000}
}
print(employee_info.keys())I executed the above example code and added the screenshot below.

In the above code, we have a dict named employee_info, and we need to get all the keys from the dictionary. So we are using the key() method like this: print(employee_info.keys()), so it is giving me all the keys from dict.
Method 3: Python Dictionary values() Method
To get all the values from the dictionary, we can use the values() method, a built-in method in Python specially made for the dict datatype to fetch the values. It will return values like this: dict_values([value1, value2,value3…]).
Syntax
dictionary.values()Let’s see how the values() method works as a Python dictionary function
emp_data = {"names": ["john", "jake", "peter", "robert"],
"age": [34,23,45,28],
"salary": [12000, 15000, 25000, 18000]}
print(emp_data.values())I executed the above example code and added the screenshot below.

In the above code, we have a dictionary named emp_data, and we need to get all the existing values from the dictionary in Python. So, we are using the values() method, which will return dict_values() and all the values inside it.
Method 4: Python Dict items() Function
When we need to access both keys and values, we can use the item() method in Python, which will return dict keys and values in the nested tuples like this dict_items([(‘key’, ‘value])
Syntax
dict.items()Example of using items() method from the Python dict methods
emp_data = {"names": ["john", "jake", "peter", "robert"],
"age": [34,23,45,28],
"salary": [12000, 15000, 25000, 18000]}
print(emp_data.items())I executed the above example code and added the screenshot below.

In the above code, we have a dict named emp_data, and we need to get all the keys and values from the dictionary. So, we are using the items() method like this: emp_data.items(), and it will return all the key-value pairs in the list of nested tuples.
Method 5: Dict.get() method of Dictionary in Python
Now, we will see how to use the get() method in Python. The get() method accesses a particular value by passing the key name as a parameter.
The benefit of using this method is that if the given key does not exist, then it will not throw any error; it will return None instead.
Syntax
dict.get("key")- dict.get(“key”): If you don’t want output as None and want to add that key into the dict, then you can give a second parameter like this dict.get(“key”, default value).
Let’s see how the get() method works from the Python dict methods.
weather_data = {
"Monday": {"temperature": 25, "humidity": 60, "wind_speed": 10},
"Tuesday": {"temperature": 22, "humidity": 65, "wind_speed": 15},
"Wednesday": {"temperature": 27, "humidity": 55, "wind_speed": 8}
}
check = weather_data.get("Wednesday")
print(check)I executed the above example code and added the screenshot below.

In the above code, we have weather data for every day. But we need to fetch the data for Wednesday only. So we are using the get() method, like this weather_data.get(“Wednesday”), which will return that particular value of the Wednesday key only.
Method 6: Use dict.fromkeys() Method in Python
The fromkeys() method is a method of the dict class in Python. It creates a new dictionary from an existing dictionary. It will take all the keys from the old dict and add them to the new one without adding values.
If you do not provide a default value in the second parameter, the default value will be none.
Syntax
var_name = dict.fromkeys(old_dict, default_value)Let’s understand the dict.fromkeys() method with a practical example.
restaurant_menu = {
"Burger": {"price": 10, "category": "Main", "is_vegetarian": False},
"Salad": {"price": 8, "category": "Appetizer", "is_vegetarian": True},
"Pizza": {"price": 12, "category": "Main", "is_vegetarian": True}
}
new_menu = dict.fromkeys(restaurant_menu)
print(new_menu)
new_menu = dict.fromkeys(restaurant_menu, "Unknown")
print(new_menu)I executed the above example code and added the screenshot below.

In the above code, we have a restaurant_menu with the items as key and their other data as a values. And we decided to change the data of items like their price.
Method 7: setdefault() Method in the Dictionary Python
The setdefault() method is also used to get the value of the given key if it exists, and if it does not exist in the dictionary, it will return None.
You can give a default value in the second parameter so that it will add a new key-value pair to the dictionary.
Syntax of setdefault() method in Python
dict.setdefault("key", default value)Let’s understand how the setdefault() method will work from Python dict methods.
car_details = {"BMW":150000,
"Audi": 120000,
"Jaguar": 140000}
car_details.setdefault("Mercedes","Not Available")
print(car_details)I executed the above example code and added the screenshot below.

We have data of car_details, and we need to make a program that if the user searches for any car, then it should show its price, and if the car is not available in the dictionary, then instead of giving key error, it should record the user search and add it into the dictionary as a key and give “Not Available” message to the user.
Method 8: Use clear() Method with Dictionary in Python
To delete all the key-value pairs from the dictionary, we can use the clear() method in Python. It can remove all the elements from the collection in Python.
Syntax
dict.clear() - clear(): This method works for every collection in Python.
Let’s see how we can use clear() method for the dictionary in Python.
product_inventory = {
"Laptop": {"brand": "Dell", "price": 1200, "stock": 50},
"Smartphone": {"brand": "Apple", "price": 1000, "stock": 100},
"Headphones": {"brand": "Sony", "price": 150, "stock": 200}
}
product_inventory.clear()
print("Product Inventory:",product_inventory)I executed the above example code and added the screenshot below.

In the above code, we have data of product_inventory, and I needed to remove all the products from the data. So we are using a clear() method like this, product_inventory.clear(), it will make the dictionary empty.
Method 9: Use the popitem() Method in Python
The popitem() method is specially made for the dictionary datatype. It removes the last key-value pair from the dictionary in Python. It does not take any arguments, so it can only remove the last key-value pair.
Syntax
dict_name.popitem()- dict_name.popitem(): If you try to use this method for different collections, it will throw an error.
customer_orders = {
"Order001": {"customer": "John", "items": ["Laptop", "Smartphone"], "total": 2200},
"Order002": {"customer": "Emily", "items": ["Headphones"], "total": 150},
"Order003": {"customer": "Michael", "items": ["Laptop", "Smartphone", "Headphones"], "total": 2350}
}
cancel_orders = customer_orders.popitem()
print("This is list of customer orders: ", customer_orders)
print("\n\nCancelled orders: ", cancel_orders)I executed the above example code and added the screenshot below.

In the above code, we took customer order records in the dictionary, and we were looking for a function that can remove the most recent order from the dictionary and also show the cancelled order to the user.
Conclusion
In this Python article, we’ve explored all the Python dict methods with practical examples. We’ve found 11 dictionary methods, such as update(), keys(), values(), items(), popitem(), etc., and explained each method individually with its syntax and code.
You may read:
- Python Dict Methods
- Convert Dict to String in Python
- Get All Values From a Dictionary in Python
- Python Get First Key in Dictionary

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.