Python dict() Function Explained
The built-in dict() function creates a new dictionary.
A dictionary stores data as key-value pairs. You use it when you want to connect one piece of data to another, such as a name to an age or a product to a price.
dict() is commonly used to:
- create an empty dictionary
- convert key-value pairs into a dictionary
- build a dictionary from keyword arguments
If you are new to dictionaries, see Python dictionaries explained.
data = dict(name='Alice', age=25)
print(data)
pairs = [('a', 1), ('b', 2)]
print(dict(pairs))
Output:
{'name': 'Alice', 'age': 25}
{'a': 1, 'b': 2}
Use dict() to create a dictionary from keyword arguments or from key-value pairs.
What dict() does
dict() creates a new dictionary object.
A dictionary:
- stores data in key-value pairs
- lets you look up values by key
- is one of the most common Python data types
dict() is a built-in Python function, so you can use it directly without importing anything.
Common uses include:
- making an empty dictionary
- converting pairs into a dictionary
- building a dictionary from keyword arguments
If you want a broader introduction to dictionary creation, see creating a dictionary in Python.
Basic syntax
dict() can be used in a few different ways.
Create an empty dictionary
data = dict()
print(data)
Output:
{}
Create a dictionary from pairs
pairs = [('a', 1), ('b', 2)]
data = dict(pairs)
print(data)
Output:
{'a': 1, 'b': 2}
Create a dictionary from keyword arguments
data = dict(name='Alice', age=25)
print(data)
Output:
{'name': 'Alice', 'age': 25}
Combine pairs or a mapping with keyword arguments
You can also start with existing data and add more values:
data = dict([('a', 1)], b=2, c=3)
print(data)
Output:
{'a': 1, 'b': 2, 'c': 3}
Create an empty dictionary
Use dict() when you want a clear function-style way to create an empty dictionary.
data = dict()
print(type(data))
print(data)
Output:
<class 'dict'>
{}
This gives the same result as:
data = {}
For empty dictionaries, {} is more common because it is shorter.
Still, dict() is useful to know because it can do more than just create an empty dictionary.
Create a dictionary from key-value pairs
You can pass an iterable of 2-item pairs to dict().
pairs = [('name', 'Alice'), ('age', 25)]
data = dict(pairs)
print(data)
Output:
{'name': 'Alice', 'age': 25}
Each pair becomes:
- the first item as the key
- the second item as the value
For example, this also works with tuples:
pairs = (('a', 1), ('b', 2))
data = dict(pairs)
print(data)
Output:
{'a': 1, 'b': 2}
Each item must contain exactly two elements.
This causes an error:
bad_data = [('a', 1, 100), ('b', 2)]
result = dict(bad_data)
Python raises an error because ('a', 1, 100) has three values, not two.
If the same key appears more than once, the later value replaces the earlier one:
pairs = [('a', 1), ('a', 99), ('b', 2)]
data = dict(pairs)
print(data)
Output:
{'a': 99, 'b': 2}
Create a dictionary with keyword arguments
You can also build a dictionary using keyword arguments:
data = dict(name='Alice', age=25)
print(data)
Output:
{'name': 'Alice', 'age': 25}
In this form:
namebecomes the string key'name'agebecomes the string key'age'
This only works when the key names are valid Python identifiers.
Valid examples:
data = dict(city='Paris', country='France')
print(data)
Invalid key names include ones with:
- spaces
- hyphens
- names that start with numbers
For example, this is not valid syntax:
# dict(first-name='Sam')
If you need keys like that, use normal dictionary syntax instead:
data = {'first-name': 'Sam', 'first name': 'Sam'}
print(data)
Output:
{'first-name': 'Sam', 'first name': 'Sam'}
dict() vs {}
Both dict() and {} create dictionaries, but they are used in slightly different situations.
Use {} when:
- you want an empty dictionary
- you want the shortest and most common syntax
data = {}
Use dict() when:
- you want to convert existing key-value pairs into a dictionary
- you want to use keyword arguments
- the function form makes the code easier to read
data = dict([('a', 1), ('b', 2)])
info = dict(name='Alice', age=25)
So:
{}is shorter for an empty dictionarydict()is more flexible when building a dictionary from other data
After creating a dictionary, you may want to add a key to a dictionary in Python or safely read values with the Python dictionary get() method.
Common errors and limits
Here are the most common problems beginners run into with dict().
Passing items that are not 2-value pairs
This is wrong:
items = ['a', 'b']
data = dict(items)
This fails because each item must be a pair like ('a', 1).
Correct version:
items = [('a', 1), ('b', 2)]
data = dict(items)
print(data)
Using invalid keyword syntax
This is not valid Python syntax:
# dict(name: 'Alice')
Keyword arguments must use =:
data = dict(name='Alice')
print(data)
Using key names that are not valid identifiers
This will not work:
# dict(first-name='Sam')
Use standard dictionary syntax instead:
data = {'first-name': 'Sam'}
print(data)
Expecting duplicate keys to be kept
If the same key appears more than once, the last value wins:
data = dict([('x', 1), ('x', 2)])
print(data)
Output:
{'x': 2}
If you are getting missing-key problems later, you may also run into KeyError in Python.
Common mistakes
Common causes of problems with dict() include:
- passing a list like
['a', 'b']instead of[('a', 1), ('b', 2)] - using invalid keyword-style keys such as
dict(first-name='Sam') - expecting duplicate keys to be kept separately
- confusing
dict()with the broader topic of dictionary creation in general
If something is not working, these quick checks can help:
print(type(data))
print(data)
print(list_of_pairs)
print(len(list_of_pairs))
These checks help you confirm:
- whether your result is really a dictionary
- what data is being passed in
- whether your list of pairs has the expected structure
FAQ
What is the difference between dict() and {}?
{} is the short literal form. dict() is a function that can also convert pairs or use keyword arguments.
Can dict() create a dictionary from a list?
Yes, if the list contains 2-item pairs such as [('a', 1), ('b', 2)].
Why does dict(name='Alice') use string keys?
In keyword form, Python turns the keyword names into string keys automatically.
Can I use spaces in keys with dict() keyword arguments?
No. For keys with spaces or other invalid identifier characters, use {'first name': 'Alice'} or pass pairs.