A Python dictionary is nothing but key-value pairs. It is a collection of unordered data values. In python, the dictionary is written in curly brackets with keys and values. In this tutorial, we will check out various Python Dictionary Methods with examples.
We will discuss here the below python dictionary methods:
- Python dictionary keys() method
- Python dictionary values() method
- get() method in Python dictionary
- add() method in Python dictionary
- Python dictionary length() method
- Python dictionary update() method
- Clear method in Python Dictionary and more
If you are new to Python check out, Python download and Installation steps and Python Hello World Program.
Python Dictionary Methods
Here we will check various python dictionary methods like keys(), values(), get(), add(), length(), update(), list() etc.
Also, we will see what is Max value in dictionary and min value in dictionary python etc.
We will check various examples of Python Dictionary Methods.
Read: Python dictionary append with examples
Python dictionary keys() method
The dictionary keys() methods in python return a new view of the dictionary keys. Any changes are done in view object then it will be reflected in the dictionary.
Example:
food = {'Tom': 'Burger', 'Jim': 'Pizza', 'Tim: 'Donut'}
f = food.keys()
print(f)
After writing the above code (python dictionary keys method), Ones you will print “ f ” then the output will appear as a “([ ‘Tom’, ‘Jim’, ‘Tim’ ]) ”. Here, keys are Tom, Jim, and Tim. You can refer to the below screenshot for creating a python dictionary keys method.
This is how to use the Keys method in Python Dictionary.
Python dictionary values() method
The Python dictionary values() method return a new view of the dictionary values. Any changes are done in view object then it will be reflected in the dictionary.
Example:
food = {'Tom': 'Burger', 'Jim': 'Pizza', 'Tim: 'Donut'}
f = food.values()
print(f)
After writing the above code (python dictionary values method), Ones you will print “ f ” then the output will appear as a “([ ‘Burger’, ‘Pizza’, ‘Donut’ ]) ”. Here, values are Burger, Pizza, and Donut. You can refer to the below screenshot for creating a python dictionary values methods.
This is an example of how to use the Python dictionary values method.
Python dictionary get() method
The Python dictionary get() method returns the value of the specified key, If the key is not found then it will return as none.
Example:
employee = {1020: 'Kim', 1021: 'Ani', 1022: 'Mishka'}
print(employee.get(1021))
After writing the above code (python dictionary get method), Ones you will print “employee.get(1021)” then the output will appear as an “ Ani ”. Here, the specified key is 1021 and its value is Ani. You can refer to the below screenshot for creating a python dictionary get methods.
If the specified key is not present then it will return as “None”.
Example:
employee = {1020: 'Kim', 1021: 'Ani', 1022: 'Mishka'}
print(employee.get(1023))
You can see that the key 1023 is not present. You can refer to the below screenshot for creating a python dictionary get methods.
Here we saw, how to use the python dictionary get method.
Python dictionary add() method
To Python add() item method in the dictionary is done by giving a new key and assigning a value to it.
Example:
employee = {1020: 'Kim', 1021: 'Ani', 1022: 'Mishka'}
employee[1023] = 'Tom'
print(employee)
After writing the above code (python dictionary add item), Ones you will print “employee” then the output will appear as an “ {1020: ‘Kim’, 1021: ‘Ani’, 1022: ‘Mishka’, 1023: ‘Tom’} ”. Here, we add 1023 as a key and its value is Tom.
You can refer to the below screenshot for creating a python dictionary add item.
This is how to use the Python dictionary add method.
Python dictionary length() method
The Python dictionary len() method is used to determine how many items (key-value pairs) a dictionary has. It gives the total length of the dictionary in Python.
Example:
employee = {1020: 'Kim', 1021: 'Ani', 1022: 'Mishka'}
print(len(employee))
After writing the above code (python dictionary length method), Ones you will print “(len(employee))” then the output will appear as a “ 3 ”. Here, the length will provide the number. You can refer to the below screenshot for creating a python dictionary length method.
This is how to use the length method in Python dictionary.
Python dictionary update() method
The Python dictionary update() method is used to update the specified item in the dictionary. The specified item can be iterable.
Example:
employee = {1020: 'Kim', 1021: 'Ani', 1022: 'Mishka'}
employee.update({1023: 'Ritika'})
print(employee)
After writing the above code (python dictionary update method), Ones you will print “(employee)” then the output will appear as a “ {1020: ‘Kim’, 1021: ‘Ani’, 1022: ‘Mishka’, 1023: ‘Ritika’} ”. Here, the update will add the specified item to the dictionary.
You can refer to the below screenshot for creating a python dictionary update method.
Here we saw how to use another Python dictionary method, and that is the Python dictionary update method.
Python dictionary list
The Python dictionary list is used to make a list in the dictionary. The value in the list could be of any type. We cannot use a dictionary of the list as a key but the same can be done with the values.
Example:
dict = {}
dict["Name"] = ["Jack"]
dict["Marks"] = [45]
print(dict)
After writing the above code (python dictionary of list), Ones you will print “(dict)” then the output will appear as a “ {‘Name’: [‘Jack’], ‘Marks’: [45] } ”. Here, we create one empty dictionary and then the value.
You can refer to the below screenshot for creating a python dictionary list.
Python dictionary comprehension
The Python dictionary comprehension uses simple expressions and concise way for creating a dictionary.
Example:
dict1 = {n:n*3 for n in range(6)}
print(dict1)
After writing the above code (python dictionary comprehension), Ones you will print “(dict1)” then the output will appear as a “{0: 0, 1: 3, 2: 6, 3: 9, 4: 12, 5: 15}”. Here, n start from 0 and it will go till 5 and it will multiply by 3 and give output as key-value pairs.
You can refer to the below screenshot for creating a python dictionary comprehension.
Check if key exists in dictionary python
In python, to check if the key exists in the dictionary, we need to specify the key is present in a dictionary using the ” in “ keyword.
Example:
my_dict = {"name": "Harry", "roll": "23", "marks": "64"}
if "marks" in my_dict:
print("Yes, 'marks' is one of the keys in dictionary")
After writing the above code (check if the key exists in dictionary python), Ones you will print then the output will appear as a “Yes, ‘marks’ is one of the keys in a dictionary”. Here, marks are present as a key in the dictionary.
You can refer to the below screenshot to check if the key exists in dictionary python.
The above code we can use to check if key exists in python dictionary.
Python dictionary remove key
In python, to remove the key from the dictionary we can use a del keyword to remove the particular key from the dictionary.
Example:
my_dict = {"name": "Harry", "roll": "23", "marks": "64"}
del my_dict["roll"]
print(my_dict)
After writing the above code (python dictionary remove key), Ones you will print ” my_dict ” then the output will appear as a ” {‘name’: ‘Harry’, ‘marks’: ’64’ } “. Here, del is used to remove the specified key from the dictionary and my specified key is ” roll ” which is removed from the dictionary.
You can refer to the below screenshot python dictionary remove key.
We can also remove the key by using the pop() method to remove the specified key elements from the dictionary.
Example:
my_dict = {"name": "Harry", "roll": "23", "marks": "64"}
my_dict.pop("roll")
print(my_dict)
After writing the above code (python dictionary remove key), Ones you will print ” my_dict ” then the output will appear as a ” {‘name’: ‘Harry’, ‘marks’: ’64’ } “. Here, pop() method is used to remove the specified key from the dictionary and the specified key is ” roll ” which is removed from the dictionary.
You can refer to the below screenshot python dictionary remove key.
Find max value in dictionary python
In python, max() value will find the key with maximum value in the dictionary.
Example:
my_dictionary = {"avinav": 11, "John": 22, "nick": 23}
maximum_key = max(my_dictionary, key=my_dictionary.get)
print(maximum_key)
After writing the above code (find max value in dictionary python), Ones you will print ” maximum_key ” then the output will appear as a ” nick “. Here, the max() value will find the key with the maximum value present in the dictionary.
You can refer to the below screenshot find max value in dictionary python
The above code we can use to find max value in dictionary in Python.
Find min value in dictionary python
In python, min() value will find the key with minimum value in the dictionary.
Example:
my_dictionary = {"avinav": 111, "John": 222, "nick": 223}
minimum_key = min(my_dictionary, key=my_dictionary.get)
print(minimum_key)
After writing the above code (find min value in dictionary python), Ones you will print ” minimum_key ” then the output will appear as an ” avinav “. Here, the min() value will find the key with the minimum value present in the dictionary.
You can refer to the below screenshot find minimum value in dictionary python
This is how to find the min value in Python dictionary.
Python dictionary clear() method
In the python dictionary we have the method clear() which will remove all the elements from the dictionary.
Example:
Student = {
"Nick": "America",
"Roll": 154,
"year": 2019
}
Student.clear()
print(Student)
After writing the above code (python dictionary clear() method), Ones you will print ” Student ” then the output will appear as an ” {} “. Here, the clear() method will remove all the elements from the Student list.
You can refer to the below screenshot python dictionary clear() method.
This is how to use the Python dictionary clear method.
In this Python tutorial, we learned about python dictionary methods with some examples.
You may like following Python tutorials:
- 11 Python list methods
- Complex Numbers in Python
- How to create a list in Python
- Python String Functions
- How to convert an integer to string in python
- How to concatenate strings in python
- How to split a string using regex in python
- Python Concatenate Dictionary
Here we checked various Python dictionary methods like:
- Python dictionary keys() method
- Python dictionary values() method
- Python dictionary get() method
- Python dictionary add() method
- Python dictionary length() method
- Python dictionary update() method
- Python dictionary list
- Check if key exists in dictionary python
- Python dictionary remove key
- Find max value in dictionary python
- Find min value in dictionary python
- Python dictionary clear() method
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.