Table of contents
1.
Introduction
1.1.
Python
2.
Python Dictionary inbuilt functions
2.1.
get()
2.2.
Python
2.3.
Python
2.4.
keys()
2.5.
Python
2.6.
Python
2.7.
copy()
2.8.
Python
2.9.
items()
2.10.
Python
2.11.
values()
2.12.
Python
2.13.
Python
2.14.
pop()
2.15.
Python
2.16.
Python
2.17.
update()
2.18.
Python
2.19.
fromkeys()
2.20.
Python
2.21.
Python
2.22.
clear()
2.23.
Python
3.
Frequently Asked Questions
3.1.
What is __dict__ Python method?
3.2.
Is dict() a method in Python?
3.3.
What are the 3 types of methods in Python?
4.
Conclusion
Last Updated: Dec 22, 2024
Easy

Methods of Dictionary

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Python is one of the most famous and used programming languages of the coding hub used on a server to create applications.

Here we will be discussing Python data type, i.e., Dictionary. Dictionary are one of the four built-in data types that store the data with different capabilities.

Methods of Dictionary

Dictionaries store the data like a map in the key: value pairs. Dictionary is represented by curly braces having keys and values. 

Example:

  • Python

Python

dict1={ “Name” : “Ashish”, “age” : 20}
print(dict1)
You can also try this code with Online Python Compiler
Run Code

Output

{‘Name’ : ‘Ashish’, ‘age’ : 20}

 

Ordered: The current version of the python language, i.e., python 3.7, dictionaries are ordered. This means that the defined order of the items will not change.

Changeable: This means we can modify or change the items after the dictionary has been created.

Does not allow duplicates: Here, this means that the two items cannot have the same key.

 

Python has provided some built-in functions to deal with dictionaries. Let's throw some light on them.

Python Dictionary inbuilt functions

get()

This function will return the value for the specified key. The syntax of this function will be:

dict1.get(key, value)

dict1: Name of the dictionary

Key: The name for which the value is to be returned.

Value: If the specified key is not found in the dictionary, the default value, i.e., None, will be returned.

Example

Input:

  • Python

Python

Person = {
 "Name": "Ashish",
 "nationality": "Indian",
 "age": 20
}
y = Person.get("age")
print(y)
You can also try this code with Online Python Compiler
Run Code

Output

20

 

If the key is not found in the dictionary, let's see the example.

 

Example

Input:

  • Python

Python

Person = {
 "Name": "Ashish",
 "nationality": "Indian",
 "age": 20
}

y = Person.get("phone")

print(y)
You can also try this code with Online Python Compiler
Run Code

Output

None

keys()

This function returns a view object that displays a list of all the keys in the dictionary in order of insertion. The syntax of this function will be:

dictionary_name.keys()

It will return all the keys in a given dictionary in the same order as mentioned.

Example1

Input:

  • Python

Python

Student = {
 "Name": "Raj",
 "section": "B",
 "Roll_no": 24
}

t = Student.keys()

print(t)
You can also try this code with Online Python Compiler
Run Code

Output

dict_keys(['Name', 'section', 'Roll_no'])

 

Example2

We can update the dictionary after the insertion using the update function.

Input:

  • Python

Python

Student = {'Name': 'Raj', 'section': 'B'}

# before updation
print("Keys before Dictionary Updation:")
keys = Student.keys()
print(keys)

Student.update({'Roll_no':37})

# after updation
print(keys)
You can also try this code with Online Python Compiler
Run Code

Output

Keys before Dictionary Updation:
dict_keys(['Name', 'section'])
dict_keys(['Name', 'section', 'Roll_no'])

copy()

This copy() function will create a copy of the original dictionary. The syntax of the function is as follows:

Dictionary_name.copy()

It will print the dictionary elements as it is mentioned.

Example

Input:

  • Python

Python

Student = {
 "Name": "Raj",
 "section": "B",
 "Roll_no": 24
}

t = Student.copy()

print(t)
You can also try this code with Online Python Compiler
Run Code

Output:

{'Name': 'Raj', 'section': 'B', 'Roll_no': 24}

items()

This function will return all the items, i.e., all the keys and the dictionary values.

The syntax of this method is as follows:

dictionary_name.items()

It will not take any parameters and return all the tuple pairs in a given dictionary.

Example

Input: 

  • Python

Python

Colors = { 'Color1': 'Blue', 'Color2': 'Green', 'Color3': 'Red' }

Total_items = Colors.items()

# Print all the items present in the Dictionary
print(Total_items)

# update one more pair in the dictionary
Colors.update({'Color4': 'White'});
print('Updated Dictionary will be:')
print(Total_items)
You can also try this code with Online Python Compiler
Run Code

Output

dict_items([('Color1', 'Blue'), ('Color2', 'Green'), ('Color3', 'Red')])
Updated Dictionary will be:
dict_items([('Color1', 'Blue'), ('Color2', 'Green'), ('Color3', 'Red'), ('Color4', 'White')])

values()

This method returns a view object that contains the dictionary's values as a list. The syntax of this function will be:

dictionary_name.values()

It will return all the values in a given dictionary.

Example 1

Input:

  • Python

Python

Dict1 = {"Name": "Ashish", "Age": "26", "Hello": "5"}
print(Dict1.values())
You can also try this code with Online Python Compiler
Run Code

Output

dict_values(['Ashish', '26', '5'])

 

Example 2

Input:

  • Python

Python

Total_costs = {"Cost1" : 700, "Cost2" : 500, "Cost3" : 489}

# total_sum will store all the values of the above dictionary
total_sum = Total_costs.values()
# sum function will calculate the sum of the values
print(sum(total_sum))
You can also try this code with Online Python Compiler
Run Code

Output

1689

pop()

This function will remove the specified item from the dictionary. The syntax of the function is as follows:

dict1.pop(key, value)

dict1: Name of the dictionary

Key: The name of the item that is to be popped from the dictionary.

Value: If the specified key is not found in the dictionary, the default value, i.e., None, will be returned.

Example1

Input:

  • Python

Python

Dict1 = {"Name": "Ashish", "Age": "26", "Hello": "5"}

Dict1.pop("Age")

print(Dict1)
You can also try this code with Online Python Compiler
Run Code

Output

{'Name': 'Ashish', 'Hello': '5'}

 

Example2

Input:

  • Python

Python

Dict1 = {"Name": "Ashish", "Age": "26", "Hello": "5"}
t = Dict1.pop("Hello")
print(t)
You can also try this code with Online Python Compiler
Run Code

Output

5

update()

The update() function will insert the key: value pairs in the dictionary. The syntax of this function will be:

Dict1.update(iterable/dictionary)

The update() function parameters could be a dictionary or an iterable key-value pair to insert it in the dictionary.

Example

Input:

  • Python

Python

Colors = {
 "Color1": "White",
 "Color2": "Blue",
 "Color3": "Red"
}

Colors.update({"Color4": "Black"})

print(Colors)
You can also try this code with Online Python Compiler
Run Code

Output

{'Color1': 'White', 'Color2': 'Blue', 'Color3': 'Red', 'Color4': 'Black'}

fromkeys()

This function will return a dictionary with the specified keys and the specified values. The syntax for this method is as follows:

DICT.fromkeys(keys, value)

Keys: It specifies the keys of the new dictionary.

Value: Default value is none.

Example

Input:

  • Python

Python

x = ('Color1', 'Color2', 'Color3')
y = 0

Dict1 = dict.fromkeys(x, y)
print(Dict1)
You can also try this code with Online Python Compiler
Run Code

Output

{'Color1': 0, 'Color2': 0, 'Color3': 0}

 

Example 2

Input:

  • Python

Python

x = ('Color1', 'Color2', 'Color3')

Dict1 = dict.fromkeys(x)
print(Dict1)
You can also try this code with Online Python Compiler
Run Code

Output

{'Color1': None, 'Color2': None, 'Color3': None}

clear()

This function will remove all the elements from the dictionary. The syntax for this will be:

dict1.clear()

It will not take any parameters and print an empty list.

Example

Input:

  • Python

Python

Dict1 = {"Name": "Ashish", "Age": "26", "Hello": "5"}
Dict1.clear()

print(Dict1)
You can also try this code with Online Python Compiler
Run Code

Output

{}

 

Must Read Python List Operations

Frequently Asked Questions

What is __dict__ Python method?

The __dict__ method in Python returns the dictionary representation of an object's namespace, showing its attributes and corresponding values.

Is dict() a method in Python?

dict() is a built-in function in Python, not a method. It creates a new dictionary from various input formats.

What are the 3 types of methods in Python?

The three types of methods in Python are instance methods (operate on object data), class methods (operate on class data), and static methods (independent of class or object).

Conclusion

In this blog, we have covered the following things:

  • What do you mean by python language?
  • What is the meaning of a Dictionary?
  • Inbuilt methods are used in the dictionary.

 

Anyone interested in learning the python language can enroll in the intuitively designed courses on Code360. You can also consider our paid courses such as DSA in Python to give your career an edge over others!

Live masterclass