Python dict() Function: Syntax and 5 Examples

If you want a dictionary of key-value pairs in Python, the Python dict function is what you’re looking for. It takes keyword arguments, pairs or another mapping and gives you back a dict.

print(dict(state="TX", zip="73301"))
# {'state': 'TX', 'zip': '73301'}

In this tutorial, I’ll take dict() apart in five examples: keywords, tuples, zip(), copies and generators. You’ll see real output from Python 3.12.5 in Command Prompt.

Python dict() Syntax

dict(**kwargs)
dict(mapping, /, **kwargs)
dict(iterable, /, **kwargs)
ParameterRequiredDefaultDescription
mappingNononeA mapping to copy
iterableNononePairs of exactly two objects
**kwargsNononekey=value pairs, added last

Return value: a new dict. With no arguments, you get an empty dictionary.

Note: the dict function in Python is really a class, but you call it like the other Python built-in functions.

When to Use dict() in Python

  • Building a record from keyword arguments
  • Turning rows from an Excel export or SQLite query into a lookup
  • Pairing two lists with zip()
  • Copying a dictionary while changing a few keys

Once you have one, the Python dictionary methods take over.

Example 1: How Do You Create a Dictionary with dict()?

Let me start with an empty call and keyword arguments:

empty = dict()
customer = dict(name="Megan Ross", city="Austin", state="TX")

print(empty, type(empty))
print(customer)

Output:

{} <class 'dict'>
{'name': 'Megan Ross', 'city': 'Austin', 'state': 'TX'}
Python dict function with keyword arguments and an empty dict() call in Command Prompt
Empty and keyword dicts.

How does this work? dict() returned {}. Each keyword name became a string key like 'city', in the order you passed them.

To format the output, see how to print a dictionary in Python.

Example 2: Convert a List of Tuples to a Dictionary

Order data often arrives as tuples. Watch the repeated ID:

orders = [("ORD-1001", 49.99), ("ORD-1002", 125.50),
          ("ORD-1003", 18.75), ("ORD-1001", 52.10)]

totals = dict(orders)
print(totals)
print(f"ORD-1002: ${totals['ORD-1002']:.2f}")

Output:

{'ORD-1001': 52.1, 'ORD-1002': 125.5, 'ORD-1003': 18.75}
ORD-1002: $125.50
Python dict converting a list of order tuples to a dictionary in Command Prompt
Last duplicate wins.

How does this work? Each tuple became one pair. ORD-1001 showed up twice, so its last value, 52.1, won.

Example 3: Make a Dict from Two Lists with zip() and dict()

Here, I’ll pair November highs (°F) with their cities:

cities = ["Denver", "Phoenix", "Seattle", "Miami"]
highs = [41, 68, 47, 79]

forecast = dict(zip(cities, highs))
print(forecast)
print("Warmest:", max(forecast, key=forecast.get))

Output:

{'Denver': 41, 'Phoenix': 68, 'Seattle': 47, 'Miami': 79}
Warmest: Miami
Python dict with zip turning two lists of cities and temperatures into a dictionary in Command Prompt
Two lists, one dictionary.

How does this work? zip() paired each city with its high, and dict() made them keys and values. max() with key=forecast.get found Miami.

More on this in the Python zip() function and create a dictionary from two lists.

Example 4: Copy a Dictionary and Override Keys with dict()

Pass a mapping plus keywords to copy it and change some values:

defaults = {"region": "East", "currency": "USD", "tax": 0.06}

west = dict(defaults, region="West", tax=0.0725)
print(west)
print(defaults)
print(west is defaults)

Output:

{'region': 'West', 'currency': 'USD', 'tax': 0.0725}
{'region': 'East', 'currency': 'USD', 'tax': 0.06}
False
Python dict copying a defaults dictionary and overriding region and tax with keywords in Command Prompt
Original unchanged.

How does this work? Keywords are applied last, so they replaced region and tax in the copy only.

Pro Tip: this is a shallow copy. See how to copy a dictionary in Python.

Example 5: Build a Dictionary from a Generator Expression

dict() accepts any iterable of pairs, including a generator:

invoices = [
    {"id": "INV-2026-301", "amount": 1200.00, "paid": True},
    {"id": "INV-2026-302", "amount": 845.25, "paid": False},
    {"id": "INV-2026-303", "amount": 310.40, "paid": False},
]

unpaid = dict((i["id"], i["amount"]) for i in invoices if not i["paid"])
print(unpaid)
print(f"Total due: ${sum(unpaid.values()):,.2f}")

Output:

{'INV-2026-302': 845.25, 'INV-2026-303': 310.4}
Total due: $1,155.65

How does this work? The generator yielded (id, amount) pairs for unpaid invoices, totaling $1,155.65.

A Python dictionary comprehension reads better for this.

Common Mistakes with Python dict()

You’ll hit these Python errors most (see types of errors in Python):

print(type(dict), dict(["NY", "CA"]))

tests = ['dict([("TX", 1, 2)])', 'dict({"a": 1}, {"b": 2})',
         'dict([(["Austin"], 1)])']
for expr in tests:
    try:
        eval(expr)
    except (TypeError, ValueError) as err:
        print(f"{type(err).__name__}: {err}")

dict = {"zip": "73301"}
try:
    dict(city="Austin")
except TypeError as err:
    print("TypeError:", err)

Output:

<class 'type'> {'N': 'Y', 'C': 'A'}
ValueError: dictionary update sequence element #0 has length 3; 2 is required
TypeError: dict expected at most 1 argument, got 2
TypeError: unhashable type: 'list'
TypeError: 'dict' object is not callable
Common Python dict mistakes: ValueError, TypeError for extra arguments, unhashable keys and shadowing
Real dict() errors.
  1. Strings are pairs too: "NY" became 'N': 'Y'.
  2. Every item needs exactly two parts.
  3. Pass one positional argument at most.
  4. Keys must be hashable, so no lists.
  5. Never name a variable dict.

Wrapping Up the Python dict() Function

Python’s dict() builds dictionaries from keywords, pairs or a mapping. Keywords win, and so does the last duplicate key.

Next, browse more Python built-in functions and methods.

Keep exploring with these:

Python dict() Function Questions, Answered

What is a dict in Python?

It’s a dictionary: values stored under unique keys, so you look data up by name.

Do I need to import dict in Python?

No. dict is built in and always available.

Is dict() the same as {}?

Both give a dictionary; only dict() converts pairs.

Is dict a function or a class?

A class: type(dict) returns <class 'type'>.

Does dict() keep insertion order?

Yes, since Python 3.7.

Can dict() take more than one positional argument?

No, one at most. See dict() in the Python docs.