🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

dict()

AI & DATA SCIENCE // dict

dict() builds a new dictionary, either empty, from keyword arguments, or from an iterable of key-value pairs.

Syntax

dict(**kwargs)
dict(mapping)
dict(iterable_of_pairs)

Deep Dive Course

dict() is flexible about its input: calling it with keyword arguments builds a dictionary directly from them, calling it on an existing dict copies that mapping, and calling it on any iterable of 2-item pairs — including the output of zip() — builds one from those pairs. Since Python 3.7, dicts preserve insertion order as a language guarantee, not just an implementation detail.

1Understanding dict()

dict() is flexible about its input: calling it with keyword arguments builds a dictionary directly from them, calling it on an existing dict copies that mapping, and calling it on any iterable of 2-item pairs — including the output of zip() — builds one from those pairs. Since Python 3.7, dicts preserve insertion order as a language guarantee, not just an implementation detail.

💡

dict(zip(keys, values)) is the standard idiom for combining two parallel lists into a dictionary.

editor.html
person = dict(name="Alice", age=30)
print(person)
localhost:3000

2Practical Example

Here is a real-world application of dict() showing how it is used in production Python code.

editor.html
keys = ["a", "b", "c"]
values = [1, 2, 3]
combined = dict(zip(keys, values))
print(combined)
localhost:3000

3Best Practices

Follow these guidelines when working with dict():

1. Use dict.get(key, default) instead of direct key access when the key might be missing, to avoid a KeyError

2. Use dict(zip(keys, values)) to build a dict from two parallel sequences

3. Prefer a dict literal over dict() with keyword arguments when keys aren't valid Python identifiers, since keyword arguments require identifier-like keys

⚠️

Tip: dict(zip(keys, values)) is the standard idiom for combining two parallel lists into a dictionary.

editor.html
person = dict(name="Alice", age=30)
print(person)
localhost:3000

Examples

Example 01Basic Usage
person = dict(name="Alice", age=30)
print(person)
Example 02Advanced Example
keys = ["a", "b", "c"]
values = [1, 2, 3]
combined = dict(zip(keys, values))
print(combined)

Best Practices

  • Use dict.get(key, default) instead of direct key access when the key might be missing, to avoid a KeyError
  • Use dict(zip(keys, values)) to build a dict from two parallel sequences
  • Prefer a dict literal over dict() with keyword arguments when keys aren't valid Python identifiers, since keyword arguments require identifier-like keys

Interview Question

Since Python 3.7, do dictionaries preserve the order items were inserted in?

Hint: This changed relatively recently in Python's history.

Yes — as of Python 3.7, insertion order preservation is an official part of the language specification for dict (it was already true as a CPython implementation detail in 3.6). Iterating over a dict, its keys, values, or items always yields entries in the order they were added, and re-inserting an existing key updates its value in place without moving its position.

Exercises

MediumPractice using dict() in a real scenario.
View Solution
person = dict(name="Alice", age=30)
print(person)

Frequently Asked Questions

Since Python 3.7, do dictionaries preserve the order items were inserted in?

Yes — as of Python 3.7, insertion order preservation is an official part of the language specification for dict (it was already true as a CPython implementation detail in 3.6). Iterating over a dict, its keys, values, or items always yields entries in the order they were added, and re-inserting an existing key updates its value in place without moving its position.

Related Functions

dictionarieszip()dict comprehensions