Python dict()
Python dict() Function
Last Updated : 17 Mar 2025
Python dict() function is a constructor which creates a dictionary. Python dictionary provides three different constructors to a create dictionary.
- If no argument is passed, it creates an empty dictionary.
- If a positional argument is given, a dictionary is created with the same key-value pairs. Otherwise, pass an iterable object.
- If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument.
Python dict() Function Syntax
It has the following syntax:
Parameters
- kwargs: It is a keyword argument.
- mapping: It is another dictionary.
- iterable: It is an iterable object in the form of a key-value pair(s).
Return
It returns a dictionary.
Different Examples for Python dict() Function
Let's see some examples of dict() function to understand it's functionality.
Python dict() Function Example 1
A simple example to create an empty or non-empty dictionary. Arguments of the dictionary are optional.
Output:
Python dict() Function Example 2
Let us take an example to demonstrate the Python dict() Function.
Output:
{'x': 5, 'z': 20, 'y': 10}
{'x': 5, 'z': 20, 'y': 10}
Python dict() Function Example 3
Let us take an example to demonstrate the Python dict() Function.
Output:
{1: 'One', 2: 'Two', 3: 'Three'}
{'y': 'Y', 'x': 'X'}