dict() Builtin Function


Python - dict()

Python dict() builtin function is used create a dictionary from the given keyword arguments.

In this tutorial, you will learn the syntax of dict() function, and then its usage with the help of example programs.

Syntax

The syntax of dict() function is

dict(keyword_arguments)

where

ParameterDescription
keyword_argumentsComma separated key-value pairs.

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

Examples

1. 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}')

Output

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

2. 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}')

Output

Dictionary : {}

Summary

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