VOOZH about

URL: https://www.geeksforgeeks.org/python/dict-constructor-in-python/

⇱ dict() Constructor in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

dict() Constructor in Python

Last Updated : 23 Jul, 2025

In Python, the dict() constructor is used to create a dictionary object. A dictionary is a built-in data type that stores data in key-value pairs, similar to a real-life dictionary where each word (the key) has a meaning (the value). The dict() constructor helps you create empty dictionaries or convert other types of data, like lists or tuples, into dictionaries.

Example:


Output
{} <class 'dict'>
{1: 'one', 2: 'two'} <class 'dict'>
{'x': 10, 'y': 20, 'z': 30} <class 'dict'>

Syntax of dict()

dict([iterable],**kwargs)
  • iterable: This is an optional argument. If you pass an iterable (like a list or tuple) of key-value pairs, dict() will convert it into a dictionary.
  • kwargs: These are keyword arguments. You can pass keys and values directly as named parameters when creating the dictionary.
  • If no arguments are passed, the constructor will return an empty dictionary.

Use of dict() Constructor

The dict() constructor is useful for:

  • Creating an empty dictionary that you can fill later.
  • Converting other data types (like lists, tuples, and ranges) into dictionaries.
  • Creating a dictionary from keyword arguments where you specify the keys and their corresponding values.

Let's go through some examples to understand it better.

Create an Empty Dictionary

This creates an empty dictionary. You can add key-value pairs to this dictionary later, as needed.


Output
{}

Convert a List of Tuples to a Dictionary

Here, we use a list of tuples where each tuple contains a key and a value. The dict() constructor takes this list and turns it into a dictionary.


Output
{1: 'apple', 2: 'banana', 3: 'cherry'}

Create a Dictionary Using Keyword Arguments

Another way to create a dictionary is by using keyword arguments. This is an easy way to define a dictionary when you already know the keys and values.


Output
{'name': 'Alice', 'age': 30, 'city': 'New York'}

Create Nested Dictionaries

You can also create nested dictionaries (dictionaries inside dictionaries)


Output
{'student1': {'name': 'John', 'age': 21}, 'student2': {'name': 'Emma', 'age': 22}}
Comment
Article Tags: