Python dictionary type() Method



The Python dictionary type() method is used to retrieve the type of the variable passed. If the variable passed is a dictionary, then it will return a dictionary type.

A dictionary is a mapping data type in Python. A data type made up of a collection of keys and its corresponding values is known as a mapping type. Mappings are mutable objects, meaning that its state can be modified after it is created.

Syntax

Following is the syntax of Python dictionary type() method −

type(dict)

Parameters

  • dict − This is the dictionary.

Return Value

This method returns the type of the variable passed.

Example

The following example shows the usage of Python dictionary type() method. Here a dictionary 'dict' is created. Then the type of the variables passed in the dictionary is retrieved using the type() method.

# creating a dictionary
dict = {'Name': 'Zara', 'Age': 7};
# printing the result
print ("Variable Type : %s" %  type (dict))

When we run above program, it produces following result −

Variable Type : <class 'dict'>

Example

In here, we are creating an empty dictionary 'dict_1'. Then the type of an empty dictionary is returned using type() method.

# Creating an empty dictionary
dict_1 = {};
res = type(dict_1)
# Printing the result
print ("The equivalent string is: ", res)

Following is an output of the above code −

The equivalent string is:  <class 'dict'>

Example

In the following example we are creating the list of a nested dictionary 'dict_1'. Then the type of this dictionary is returned using type() method.

dict_1 = [{'Universe' : {'Planet' : 'Earth'}}]
print("The dictionary is: ", dict_1)
# using type() method
result = type(dict_1)
print("The equivalent string is: ", result)

Output of the above code is given below: −

The dictionary is:  [{'Universe': {'Planet': 'Earth'}}]
The equivalent string is:  <class 'list'>
python_dictionary.htm
Advertisements