Python dict() Function Explained

By
Bosko Marijan
Published:
April 1, 2026
Topics:

The dict() function is a built-in Python constructor that creates dictionaries. It provides a flexible way to build key-value collections from various sources, such as keyword arguments, iterable pairs, or existing mappings.

Unlike literal syntax {}, dict() is especially useful when converting data into dictionary form or when dynamically constructing key-value pairs.

This article will explain the dict() syntax, its parameters, and show practical examples of how to use it in different scenarios.

Python dict() function explained.

Prerequisites

Python dict() Syntax Explained

The dict() function supports multiple input formats, allowing users to construct dictionaries from different data sources. Understanding these formats helps you choose the most efficient approach depending on your data's structure.

The available formats are:

dict(**kwargs)
dict(mapping, **kwargs)
dict(iterable, **kwargs)

Each format is suited for different input types: keyword arguments for static data, mappings for copying, and iterables for transforming structured data.

Each syntax variation creates a new dictionary and optionally merges multiple inputs into a single result. When combining inputs, the mapping or iterable is processed first, and keyword arguments are applied afterward, overriding any duplicate keys.

dict() Parameter Values

The dict() function accepts different parameters that define how the dictionary is created. These parameters allow users to pass data directly, copy existing mappings, or transform iterable data into key-value pairs.

The parameter types are:

  • kwargs. Key-value pairs passed as keyword arguments. Keys must be valid identifiers (strings). However, dictionaries themselves can contain keys of any hashable type (e.g., strings, numbers, tuples).
  • mapping. An existing mapping object (such as another dictionary). The function copies its key-value pairs.
  • iterable. An iterable object containing key-value pairs. Each element must be an iterable containing exactly two items (e.g., tuples, lists, or other pair-like structures).

The parameters can be combined. If duplicate keys exist, later values override earlier ones.

Python dict() Examples

The following sections demonstrate how to use the dict() function in real-world scenarios. Each example highlights a different input method and shows when it is most useful. Run the code in the Python interpreter, or paste it into a script and run it.

Note: To start the Python interpreter, run python3 in the terminal if you are using Linux, or python in the Command Prompt if you are on Windows. To exit, run exit().

Create a Dictionary Using Keyword Arguments

Use keyword arguments when you know the keys in advance and want a clean, readable way to define a dictionary. This approach is concise but requires keys to follow Python identifier rules (no spaces or special characters).

Run the code below:

user = dict(name="John", age=33, country="US")
print(user)
Create a dictionary using keyword arguments in dict().

This output shows that dict() created a dictionary using the provided keyword arguments as key-value pairs. This approach is useful when defining configuration-like data directly in code.

Note: See how to add items to a Python dictionary using different methods.

Create a Dictionary From a List of Tuples

Use an iterable of key-value pairs when your data is already structured as pairs, such as when processing external input or transforming datasets. Each tuple must contain exactly two elements: a key and a value.

For example:

data = [("name", "John"), ("age", 33), ("country", "US")]
user = dict(data)
print(user)
Creating a dictionary from a list of tuples.

The output confirms that dict() converted each tuple in the list into a corresponding key-value pair in the dictionary. This is useful when working with structured data such as CSV rows or database query results.

Create a Dictionary From Another Dictionary

Pass an existing dictionary when you want to create a shallow copy before modifying the data. Creating a shallow copy means that the top-level dictionary is copied, but nested mutable objects are still shared between the original and the copy.

Use the code below:

original = {"name": "John", "age": 33}
copy = dict(original)

print(copy)
Create a dictionary from another dictionary using dict().

In the example above, dict() created a shallow copy of the original dictionary. This is commonly used when you want to modify data without affecting the original source.

Combine Multiple Sources

You can also use dict() to merge data from multiple sources into a single dictionary. This feature is useful when updating or enriching existing data. If the same key appears multiple times, the last value provided overrides earlier ones.

For example:

base = {"name": "John", "age": 30}
updated = dict(base, age=33, country="Serbia")

print(updated)
Combine multiple sources with Python dict().

Here, dict() merged the original dictionary with new values, overriding duplicate keys with the latest provided values. This is useful when updating user data or applying configuration overrides.

Conclusion

This tutorial showed how to use the Python dict() function to create dictionaries from keyword arguments, mappings, and iterable key-value pairs. Its flexibility makes it useful when transforming or combining data into dictionary form, giving you more control than basic literal syntax.

Next, check out our tutorial for the Python randint() method.

Was this article helpful?
YesNo