Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Class

dict

dict is Python’s mutable mapping type, associating hashable keys with arbitrary values. It supports construction from mappings, key-value pairs, and keyword arguments, plus dynamic key, value, and item views.

How it works

dict is Python’s mutable mapping type: it associates hashable keys with arbitrary values. Construct one from keyword arguments, another mapping, or an iterable of key-value pairs; access and modify entries with subscription, while keys(), values(), and items() provide dynamic views.

Syntax

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

Parameters

NameTypeDescription
kwargskeyword argumentsKeyword arguments used to create or update entries; keyword keys must be valid Python identifiers.
mappingmappingA mapping whose entries are copied into the new dictionary.
iterableiterable of pairsAn iterable producing key-value pairs.

Returns

A new dictionary object when called as a constructor.

Key points

  • A dictionary associates hashable keys with arbitrary values.
  • The three collection methods return view objects for keys, values, and key-value pairs.
  • Dictionary views track changes made to their underlying dictionary.
  • Distinct objects that compare equal can refer to the same dictionary entry as keys.

Common mistakes

  • Keys must be hashable, so mutable objects such as lists and dictionaries cannot be used as keys.
  • Keyword-based construction cannot represent arbitrary string keys; use a mapping or pairs when a key is not a valid identifier.
  • Values views do not compare equal to one another, even when they expose the same values.

Example: Create and update a dictionary

scores = {"Ada": 10, "Grace": 12}
scores["Ada"] += 1
scores.setdefault("Lin", 0)

for name, score in scores.items():
    print(name, score)

Creates a mutable dictionary, updates an existing entry, initializes a missing key, and iterates over its item view.

When to use it

Structured lookup

Representing keyed application data such as configuration or records.

Mutable state

Counting, caching, or updating values by an identifier.

Dynamic views

Iterating over keys, values, or pairs while observing later dictionary changes.

Interview questions

QWhat is a Python dict?

A dictionary maps hashable keys to arbitrary values and is mutable. Keys identify entries, while values can be any objects.

Follow-up: What happens when two keys compare equal? They address the same dictionary entry; values such as 1, 1.0, and True can be interchangeable keys.

QHow can a dictionary be constructed?

The constructor accepts keyword arguments, a mapping plus keyword arguments, or keyword arguments alone. It can also consume an iterable of key-value pairs.

Follow-up: When should you avoid keyword construction? Use another form when keys are not valid Python identifiers or when preserving arbitrary key objects matters.

QWhat do dictionary view methods return?

keys(), values(), and items() return view objects rather than independent snapshots. Those views dynamically reflect later changes to the dictionary.

Follow-up: Which view has the unusual equality behavior? Comparing one dict.values() view with another always produces False.

  • dict.keys()
  • dict.values()
  • dict.items()
  • dict.setdefault()
  • Mapping

Source: Built-in Types — Python 3.10.21 documentation. Checked 2026-10-15.