Python dict() — A Simple Guide with Video

Python’s built-in dict() function creates and returns a new dictionary object from the comma-separated argument list of key = value mappings. For example, dict(name = 'Alice', age = 22, profession = 'programmer') creates a dictionary with three mappings: {'name': 'Alice', 'age': 22, 'profession': 'programmer'}. A dictionary is an unordered and mutable data structure, so it can be changed after creation.

Read more about dictionaries in our full tutorial about Python Dictionaries.

Python dict() Visual Explanation

Usage

Learn by example! Here are some examples of how to use the dict() built-in function:

>>> dict(name = 'Alice')
{'name': 'Alice'}
>>> dict(name = 'Alice', age = 22)
{'name': 'Alice', 'age': 22}
>>> dict(name = 'Alice', age = 22, profession = 'programmer')
{'name': 'Alice', 'age': 22, 'profession': 'programmer'}

You can pass an arbitrary number of those comma-separated key = value pairs into the dict() constructor.

Video dict()

Syntax dict()

You can use the dict() method with an arbitrary number of key=value arguments, comma-separated.

Syntax: There are four ways of using the constructor:
dict() -> new empty dictionary 
dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs
dict(iterable) -> new dictionary initialized from an iterable of (key, value) tuples
dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list.

Interactive Shell Exercise: Understanding dict()

Consider the following interactive code:

Exercise: Guess the output before running the code.


But before we move on, I’m excited to present you my new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


The dict() function has many different options to be called with different types of arguments. You’ll learn different ways to use the dict() function next.

How to Create an Empty Dictionary?

You can create an empty dictionary by using Python’s built-in dict() function without any argument. This returns an empty dictionary. As the dictionary is a mutable data structure, you can add more mappings later by using the d[key] = value syntax.

>>> d = dict()
>>> d['Alice'] = 22
>>> d
{'Alice': 22}

How to Create a Dictionary Using Only Keyword Arguments?

You can create a dictionary with initial key: value mappings by using a list of comma-separated arguments such as in dict(name = 'Alice', age = 22) to create the dictionary {'name': 'Alice', 'age': 22}. These are called keyword arguments because each argument value has its associated keyword.

>>> dict(name = 'Alice', age = 22)
{'name': 'Alice', 'age': 22}

How to Create a Dictionary Using an Iterable?

You can initialize your new dictionary by using an iterable as an input for the dict(iterable) function. Python expects that the iterable contains (key, value) pairs. An example iterable is a list of tuples or a list of lists. The first values of the inner collection types are the keys and the second values of the inner collection types are the values of the new dictionary.

>>> dict([(1, 'one'), (2, 'two')])
{1: 'one', 2: 'two'}
>>> dict([[1, 'one'], [2, 'two']])
{1: 'one', 2: 'two'}
>>> dict(((1, 'one'), (2, 'two')))
{1: 'one', 2: 'two'}

Note that you can use inner tuples, inner lists, outer tuples or outer lists—as long as each inner collection contains exactly two values. If it contains more, Python raises an ValueError: dictionary update sequence element.

>>> dict([(1, 'one', 1.0), (2, 'two', 2.0)])
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    dict([(1, 'one', 1.0), (2, 'two', 2.0)])
ValueError: dictionary update sequence element #0 has length 3; 2 is required

You can fix this ValueError by passing only two values in the inner collections. For example use a list of tuples with only two but not three tuple elements.

How to Create a Dictionary Using an Existing Mapping Object?

If you already have a mapping object such as a dictionary mapping keys to values, you can pass this object as an argument into the dict() function. Python will then create a new dictionary based on the existing key: value mappings in the argument. The resulting dictionary will be a new object so if you change it, the changes are not reflected in the original mapping object.

>>> d = {'Alice': 22, 'Bob': 23, 'Carl': 55}
>>> d2 = dict(d)
>>> d
{'Alice': 22, 'Bob': 23, 'Carl': 55}

If you now change the original dictionary, the change is not reflected in the new dictionary d2.

>>> d['David'] = 66
>>> d
{'Alice': 22, 'Bob': 23, 'Carl': 55, 'David': 66}
>>> d2
{'Alice': 22, 'Bob': 23, 'Carl': 55}

How to Create a Dictionary Using a Mapping Object and Keyword Arguments?

Interestingly, you can also pass a mapping object into the dict() function and add some more key: value mappings using keyword arguments after the first mapping argument. For example, dict({'Alice': 22}, Bob = 23) creates a new dictionary with both key:value mappings {'Alice': 22, 'Bob': 23}.

>>> dict({'Alice': 22}, Bob = 23)
{'Alice': 22, 'Bob': 23}
>>> dict({'Alice': 22}, Bob = 23, Carl = 55)
{'Alice': 22, 'Bob': 23, 'Carl': 55}

How to Create a Dictionary Using an Iterable and Keyword Arguments?

Similarly, you can also pass an iterable of (key, value) tuples into the dict() function and add some more key: value mappings using keyword arguments after the first mapping argument. For example, dict([('Alice', 22)], Bob = 23) creates a new dictionary with both key:value mappings {'Alice': 22, 'Bob': 23}.

>>> dict([('Alice', 22)], Bob = 23)
{'Alice': 22, 'Bob': 23}
>>> dict([('Alice', 22), ('Carl', 55)], Bob = 23)
{'Alice': 22, 'Carl': 55, 'Bob': 23}

Summary

Python’s built-in dict() function creates and returns a new dictionary object from the comma-separated argument list of key = value mappings.

For example, dict(name = 'Alice', age = 22, profession = 'programmer') creates a dictionary with three mappings: {'name': 'Alice', 'age': 22, 'profession': 'programmer'}.

>>> dict(name = 'Alice', age = 22, profession = 'programmer')
{'name': 'Alice', 'age': 22, 'profession': 'programmer'}

A dictionary is an unordered and mutable data structure, so it can be changed after creation.

I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:


Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!