Python Dictionary Methods + Examples

By: On:

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.

If you are new to Python check out the below python tutorials:

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.

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.

Python Dictionary Methods
Python dictionary keys() method

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.

Python dictionary values method
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.

Python dictionary get methods
Python dictionary get() method

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.

Python dictionary get methods

Python dictionary add item

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.

Python dictionary add item
Python dictionary add item

Python dictionary length() method

The Python dictionary len() method is used to determine how many number of items (key-value pairs) a dictionary has. It gives the total length of the dictionary.

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.

Python dictionary length() method
Python dictionary length() method

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.

python dictionary update method
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 list
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.

Python dictionary comprehension
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.

Check if key exists in dictionary python
Check if key exists in dictionary python

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.

Python dictionary remove key
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.

Python dictionary remove key
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

Find max value in dictionary python
Find max value in dictionary 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

Find min value in dictionary python
Find min value in dictionary python

In this Python tutorial, we learned about python dictionary methods with some examples.


You may like following Python tutorials:

Here we checked various Python dictionary methods like:

  • Python dictionary keys() method
  • Python dictionary values() method
  • Python dictionary get() method
  • Python dictionary add item
  • 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

Leave a Comment