Methods and Functions

Python dict ()

The dict() python function is a constructor that is used to create dictionaries. Dictionary is a data structure in Python that contains elements in the form of a value and its respective key. It is a collection of data that is unordered, indexed, and can be changed after the definition of the dictionary. 

dict() pythonSource

For representation, the key and its value are separated with a colon (: ). If a dictionary is empty, then it is represented by two curly braces with no data in between, that is, {}. 

The keys that are provided to each value in a dictionary are unique to that particular value. The values stored in the dictionary can be of any data type. However, the keys must either be of the data type string, numbers, or tuples.  

The syntax of the dict () Python method is as follows. 

dict (keyword arguments) 

Here, the parameter keyword arguments refer to the elements of the dictionary. Any number of arguments can be passed in the dict () method to create a dictionary.  

The dict () function can be used in the following formats.

class dict (**kwarg)

class dict (mapping, **kwarg)

class dict (iterable, **kwarg)

Here, **kwarg allows the user to take any number of keyword arguments, as they want. Before the keyword argument, an identifier is mentioned. Thus, if the keyword argument is in the form of kwarg = value, then it will let the dict () constructor create dictionaries of the required length. 

The dict () Python method does not return any value. 

The following program displays how the method can be used in different forms. 

Example 1: Create Dictionary Using Keyword Arguments Only 


num = dict (x = 10, y = 15)

print (“numbers = “, num)

print (type (num))

empty = dict ()

print (“empty =”, empty)

print (type (empty))

Here, you will obtain an empty dictionary in the output. It will be represented by {}. 

Example 2: Create Dictionary Using Iterable 

In the program given below, we first do not pass any keyword argument in the dict () method. Once this output is obtained, another dict () method is used to pass a keyword argument. 


num = dict ([ ‘x’ , 10), (‘y’, 15)])

print (“Numbers = “, num)

num1 = dict ([ ‘x’ , 10), (‘y’, 15)], z = 9)

print (“Numbers = “, num1)

num2 = dict (dict (zip ([‘x’, ‘y’, ‘z’], [1, 4, 8]

print (“Numbers =”, num2)

The program will give three dictionaries with 2, 3 and 3 elements respectively. Note that, the first dictionary does not have any keyword argument.   

Example 3: Create Dictionary Using Mapping


num = dict ({ ‘x’: 10, ‘y’: 15})

print (“Numbers = “, num)

num1 = { ‘x’: 10, ‘y’: 15}

print (“Numbers = “, num1)

num2 = dict ({ ‘x’ :5, ‘y’: 6}, ‘z’ = 8)

print (“Numbers =”, num2)

The program uses mapping to create a dictionary using the dict () constructor. For mapping, the key is mapped with its value using the colon (:). 

Frequently Asked Questions 

What does dict () do in Python?

The dict () method in Python is a constructor that is used to create dictionaries. Depending upon the parameters that are passed in the function, a dictionary of any length can be created using the constructor.  

What is __dict__ in Python?

The __dict__ in Python is a dictionary or other such mapping objects that are used to store the attributes of an object. Every object that contains an attribute in Python can be denoted by __dict__. This object dict will contain all the attributes that are used or defined for any object. 

What is dict class in Python?

In Python, class dict refers to the class that stores data structures in the form of pairs of keys and values. The dictionaries that are created in Python are the objects of the class dict. It is an unordered collection of data that is recognised by a unique key. The keys are used to index the values stored in the dictionaries.   

How do I display a dictionary in Python?

To display a dictionary in Python, various methods can be used. For example, FOR loop can be used to call and print each element present in the dictionary. The elements are called using their unique keys.  

Share with friends

Customize your course in 30 seconds

Which class are you in?
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Dr. Nazma Shaik
Gaurav Tiwari
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.