dict() Builtin Function

Python – dict()

Python dict() builtin function returns a dictionary from the given keyword arguments, if any.

Syntax

The syntax of dict() function is

dict(keyword_arguments)

where keyword_arguments are comma separated key-value pairs.

As many keyword arguments can be given to dict() function, as required.

Examples

dict() with keyword arguments

In the following program, we create a dictionary by passing keyword arguments to dict() function.

Python Program

output = dict(name='apple', quantity=56, refurbished=True)
print(f'Dictionary : {output}')
Run

Output

Dictionary : {'name': 'apple', 'quantity': 56, 'refurbished': True}

dict() with no keyword arguments

In the following program, we create an empty dictionary by passing no keyword arguments to dict() function.

Python Program

output = dict()
print(f'Dictionary : {output}')
Run

Output

Dictionary : {}

Summary

In this tutorial of Python Examples, we learned the syntax of dict() function, and how to create a dictionary, with examples.