Python Reference Library →

Python dict() Function – Create and Use Dictionaries Easily

Let’s look at dict() function – create and use dictionaries easily

  1. Notice

    dict() creates a mapping from keys to values. A later assignment to a key replaces its earlier value.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
scores = dict(Ada=3, Ben=2)
print(scores)
Live state

Top-level scope

scores→Object 1
Object 1 · dict{'Ada': 3, 'Ben': 2}
This step

dict() creates a mapping from keys to values. A later assignment to a key replaces its earlier value.

Picture dict() function – create and use dictionaries easily

dict() creates a mapping from keys to values. A later assignment to a key replaces its earlier value.

Image brief

Alt text
A diagram for Python dict() Function – Create and Use Dictionaries Easily. Instructions: scores = dict(Ada=3, Ben=2) print(scores). Displayed result: {'Ada': 3, 'Ben': 2}. The final example bindings are scores --> object 1 (dict). object 1: {'Ada': 3, 'Ben': 2}
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python dict() Function – Create and Use Dictionaries Easily". Teaching focus: dict() creates a mapping from keys to values. A later assignment to a key replaces its earlier value. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS scores = dict(Ada=3, Ben=2) print(scores) | v DISPLAYED RESULT {'Ada': 3, 'Ben': 2} FINAL BINDINGS scores --> object 1 (dict) object 1: {'Ada': 3, 'Ben': 2} Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python dict() Function – Create and Use Dictionaries Easily. Instructions: scores = dict(Ada=3, Ben=2) print(scores). Displayed result: {'Ada': 3, 'Ben': 2}. The final example bindings are scores --> object 1 (dict). object 1: {'Ada': 3, 'Ben': 2}

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
scores = dict(Ada=3, Ben=2)
print(scores)
Live state

Top-level scope

scores→Object 1
Object 1 · dict{'Ada': 3, 'Ben': 2}
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
{'Ada': 3, 'Ben': 2}

What do you expect this program to print?

What does this example display?

scores = dict(Ada=3, Ben=2)
print(scores)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

{'Ada': 3, 'Ben': 2}

Follow the printed values in order. The output is: {'Ada': 3, 'Ben': 2}

More examples and reference

Python dict() Function

The dict() function in Python is used to create a dictionary. A dictionary is an unordered collection of key-value pairs, where each key must be unique and immutable.

Syntax

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

Parameters:

  • **kwargs – Key-value pairs as keyword arguments (e.g., name="Alice")
  • mapping – Another dictionary or mapping object
  • iterable – An iterable with key-value pairs, like a list of tuples

Returns:

  • A new dictionary object.

Example 1: Creating an Empty Dictionary

d = dict()
print(d)
{}

Example 2: Using Keyword Arguments

person = dict(name="Alice", age=25)
print(person)
{'name': 'Alice', 'age': 25}

Example 3: From List of Tuples

pairs = [("a", 1), ("b", 2)]
d = dict(pairs)
print(d)
{'a': 1, 'b': 2}

Example 4: From Another Dictionary

original = {'x': 10, 'y': 20}
copy = dict(original)
print(copy)
{'x': 10, 'y': 20}

Use Case: Why Use dict()?

  • To dynamically create a dictionary from data
  • To convert other mappings or iterable pairs into a dictionary
  • To copy an existing dictionary

Common Mistakes

  • Keys must be unique: Later keys overwrite earlier ones
  • Keys must be hashable: Lists or other dictionaries cannot be keys
  • Syntax error: Trying to use non-string keys with keyword arguments (e.g., dict(1="a") is invalid)

Interview Tip

The dict() function is often used to build frequency maps or group data. Understanding how to use dict() from tuples or with zip() is useful in many coding interviews.

Summary

  • dict() creates dictionaries from keyword arguments, mappings, or iterable pairs
  • Dictionaries store key-value pairs
  • Keys must be unique and immutable

Practice Problem

Convert two lists into a dictionary using dict() and zip():

keys = ["name", "age", "city"]
values = ["Alice", 25, "New York"]

result = dict(zip(keys, values))
print(result)
{'name': 'Alice', 'age': 25, 'city': 'New York'}

Try changing the values and see how the dictionary updates!

What to remember

Connect what you noticed in the example with the idea below.

Remember

dict() creates a mapping from keys to values. A later assignment to a key replaces its earlier value.

Watch out for this idea

A dictionary permits multiple simultaneous values for the same key.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →