Python dict() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

Definition

In Python, dict() creates a dictionary, a mapping from unique, hashable keys to values. You can pass keyword arguments (dict(name="Ana")), an iterable of key/value pairs, another mapping, or nothing for an empty dict.

Since Python 3.7, dicts keep insertion order: keys come out in the order they were first added. Lookups by key are fast on average. Use d.get(key, default) to read a key that may be missing.

dict is a built-in type (a class) that you call like a function: each call creates a new dict object. Like every built-in, it lives in the builtins module and is always available without an import.

Syntax

Four common ways to call it:

python
a = dict(name="Ana", age=30)
b = dict([("x", 1), ("y", 2)])
c = dict(zip(keys, values))
d = dict(other, extra=1)
CallWhat it does
dict(a=1, b=2)Returns {'a': 1, 'b': 2}.
dict([("a", 1)])Builds from key/value pairs.
dict(zip(ks, vs))Pairs up two lists.
dict(d)A shallow copy of another dict.

Keyword arguments only work for keys that are valid identifiers; use pairs or a literal for keys like "first name" or 1.

Library declarations
python
dict(**kwargs)
dict(mapping, /, **kwargs)
dict(iterable, /, **kwargs)
# class dict: a mutable built-in type

Parameters

ParameterDescription
mappingOptional. Another mapping whose pairs are copied.
iterableOptional. An iterable of two-item pairs (key, value).
**kwargsOptional. Keys as keyword arguments; they override earlier values.

Return Value

Return ValueDescription
dictA new dictionary.

Python Examples

Example 1 — Five Ways to Build a Dictionary

Create dicts from keywords, pairs, zip(), another dict with overrides, and nothing.

python
a = dict(name="Ana", age=30)
b = dict([("x", 1), ("y", 2)])
c = dict(zip(["red", "green"], ["#f00", "#0f0"]))
d = dict(a, age=31, city="Oslo")

print(a)
print(b)
print(c)
print(d)
print(dict())

How It Works

  1. Keywords become string keys. dict(name="Ana", age=30) is the same as the literal {"name": "Ana", "age": 30}.
  2. Pairs and zip. Any iterable of two-item pairs works, including zip(keys, values).
  3. Copy with overrides. dict(a, age=31, city="Oslo") copies a, replaces age, and adds city at the end.
  4. Order kept. Updated keys keep their original position; new keys go last.

Example 2 — Count Words and Group Them by First Letter

Use get() for counters and setdefault() for lists, and see insertion order in the output.

python
words = ["apple", "bob", "avocado", "cherry", "banana", "apple"]

counts = {}
for w in words:
    counts[w] = counts.get(w, 0) + 1
print(counts)

by_letter = {}
for w in words:
    by_letter.setdefault(w[0], []).append(w)
print(by_letter)
print(list(by_letter))

How It Works

  1. Counter pattern. counts.get(w, 0) + 1 starts new words at 0, so apple ends at 2.
  2. Grouping pattern. setdefault(key, []) creates the list the first time and returns it every time.
  3. First-seen order. Keys appear in the order they were first inserted: a, b, c.
  4. Keys as a list. list(by_letter) returns just the keys.

Example 3 — Missing Keys, Membership, and Unhashable Keys

Read keys safely, test membership, and see which values cannot be keys.

python
inventory = dict(apples=5, pears=0)

print(inventory.get("plums"))
print(inventory.get("plums", 0))
try:
    inventory["plums"]
except KeyError:
    print("missing key: use get() or 'in'")

print("pears" in inventory, 0 in inventory)

try:
    dict([(["a"], 1)])
except TypeError:
    print("list keys are unhashable")

How It Works

  1. get() never raises. A missing key returns None, or the default you pass.
  2. Indexing raises. inventory["plums"] raises KeyError.
  3. in checks keys. "pears" in inventory is True even though its value is 0; 0 in inventory looks for a key 0.
  4. Keys must be hashable. A list key raises TypeError; use a tuple instead.

Frequently Asked Questions

Use dict(zip(keys, values)). zip pairs the items, and dict turns the pairs into entries.
Yes. Since Python 3.7, dicts are guaranteed to preserve insertion order.
Use d.get(key, default), check key in d first, or use collections.defaultdict for counters and groups.
Both create a dict. {} is a literal and slightly faster; dict() is handy for keyword arguments, pairs, and copies.
Use {**a, **b} on Python 3.5+, a | b on Python 3.9+, or a.update(b) to change a in place.

Next: dir()

List the attribute names of an object or scope.

dir() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful