DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

Python dict() Function: A Practical Guide With Examples

Python’s dict() builds dictionaries from mappings, pairs, or keyword arguments. Learn its syntax, overwrite behavior, ordering, and when literals or update() are clearer.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python’s dict() function creates a new dictionary. Use it with no arguments for an empty dictionary, with a mapping or iterable of key-value pairs to copy or convert data, or with keyword arguments for identifier-like string keys. If a key appears more than once, the later value replaces the earlier one.

What does dict() do?

dict() is Python’s dictionary constructor. A dictionary stores values under keys. The Python 3.14 Built-in Types documentation defines the constructor as returning “a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.”

Its documented forms are dict(**kwargs), dict(mapping, **kwargs), and dict(iterable, **kwargs). The positional argument is optional.

Create an empty dictionary

Call dict() without arguments:

settings = dict()

The empty dictionary literal {} creates the same kind of object and is often the clearest choice when writing dictionary data directly.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Build a dictionary from existing data

From a mapping

Pass a mapping, such as another dictionary, to create a new dictionary with its key-value data:

from_mapping = dict({"debug": True, "retries": 3})

From an iterable of pairs

A positional input that is not a mapping must be an iterable whose items each contain exactly two elements: a key and its value.

from_pairs = dict([("name", "Ada"), ("language", "Python")])

Pair input can represent keys that cannot be written as keyword argument names:

labels = dict([("display name", "Ada"), ("user-id", 17)])

Each item must have exactly two elements. An item with too few or too many elements does not meet the constructor’s pair format.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

From keyword arguments

Keyword arguments provide a compact form when the keys are valid Python identifiers:

from_keywords = dict(debug=True, retries=3)

Because Python parses these names as keyword arguments, this syntax cannot express keys such as "display name" or "user-id". Use a mapping or pairs for those keys.

When a literal or comprehension is clearer

For key-value data written directly in source code, a dictionary literal makes the contents visible without constructor syntax:

person = {"name": "Ada", "language": "Python"}

When values are generated from a sequence or calculation, a dictionary comprehension expresses that transformation:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
squares = {number: number * number for number in range(4)}

Choose according to the input: literals suit inline data, comprehensions suit generated entries, and dict() is useful for mappings, pair iterables, or keyword-based construction.

Duplicate keys: later values win

If the same key is provided more than once, Python keeps the last value rather than raising an error. This applies to repeated pairs and to keyword arguments supplied alongside a positional mapping or iterable.

options = dict([("mode", "safe"), ("mode", "fast")])
# {'mode': 'fast'}

options = dict({"mode": "safe"}, mode="fast")
# {'mode': 'fast'}

This behavior lets keyword arguments override defaults, but it can also hide accidental duplicate data.

Dictionary order after construction

Dictionaries preserve insertion order. Replacing the value for an existing key leaves that key in its current position. If you delete a key and later insert it again, it appears at the end.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use update() or merge operators for existing dictionaries

dict() creates a new dictionary. If the goal is to add or replace entries in an existing one, use update() or an in-place merge instead.

Operation Effect Returns Version note
target.update(data) Mutates target; accepts a mapping or iterable of key-value pairs and overwrites matching keys. None No version restriction noted here.
left | right Creates a merged dictionary. A new dictionary Added in Python 3.9.
left |= right Updates left in place. In-place operation Added in Python 3.9.

Use | when you want a separate merged result, and |= or update() when you want to modify the existing dictionary. The merge operators are not available in Python versions earlier than 3.9.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.