Skip to content

dict

The built-in dict data type (short for dictionary) represents a collection of key-value pairs, where keys are unique and used to access corresponding values. Dictionaries are mutable, meaning they can be dynamically modified and expanded.

Here’s a basic example of creating a dictionary and accessing one of its values:

Language: Python
>>> teams = {
...     "Colorado": "Rockies",
...     "Boston": "Red Sox",
...     "Minnesota": "Twins",
...     "Milwaukee": "Brewers",
...     "Seattle": "Mariners",
... }

>>> teams["Minnesota"]
'Twins'

dict Constructors

Language: Python Syntax
dict(**kwargs)
dict(mapping, **kwargs)
dict(iterable, **kwargs)

Arguments

Argument Description
kwargs Keyword arguments where the keywords are the keys and the values are the corresponding dictionary values.
mapping A dictionary or any object with a .keys() method.
iterable An iterable of key-value pairs (tuples or iterables of length two).

Return Value

  • Returns a Python dict object

dict Examples

Creating an empty dictionary:

Language: Python
>>> empty_dict = {}
>>> empty_dict
{}

Creating a dictionary using literals:

Language: Python
>>> person = {"name": "Alice", "age": 30, "city": "New York"}
>>> person
{'name': 'Alice', 'age': 30, 'city': 'New York'}

Creating a dictionary using the constructor with keyword arguments:

Language: Python
>>> person = dict(name="Alice", age=30, city="New York")
>>> person
{'name': 'Alice', 'age': 30, 'city': 'New York'}

Accessing dictionary values:

Language: Python
>>> person["name"]
'Alice'
>>> person["age"]
30

Changing values in a dictionary through key assignment:

Language: Python
>>> person["age"] = 31
>>> person["age"]
31

Deleting values from a dictionary:

Language: Python
>>> del person["city"]
>>> person
{'name': 'Alice', 'age': 31}

dict Methods

Method Description
.clear() Removes all items from the dictionary.
.copy() Returns a shallow copy of the dictionary.
.fromkeys() Creates a new dictionary with keys from an iterable and values set to a specified value.
.get() Returns the value for a specified key or a default if the key is not found.
.items() Returns a view object displaying a list of the dictionary’s key-value pairs.
.keys() Returns a view object displaying a list of all the keys.
.pop() Removes the specified key and returns its value.
.popitem() Removes and returns the last key-value pair as a tuple.
.setdefault() Returns the value of a key if it exists; otherwise, inserts the key with a default value.
.update() Updates the dictionary with items from another dictionary or iterable of key-value pairs.
.values() Returns a view object displaying a list of all the values.

dict Common Use Cases

The most common use cases for the dict include:

  • Storing and accessing data organized in key-value pairs
  • Managing and organizing data as key-value pairs
  • Implementing caches or memoization
  • Aggregating counts or other statistics
  • Representing JSON-like data structures

dict Real-World Example

Suppose you need to manage a contact list where each contact is identified by a unique name and has associated details like phone number and email. You can use a dictionary to store and manage this data:

Language: Python
>>> contacts = {
...     "Alice": {"phone": "123-456-7890", "email": "alice@example.com"},
...     "Bob": {"phone": "987-654-3210", "email": "bob@example.com"}
... }

>>> bob_phone = contacts["Bob"]["phone"]

>>> # Update Alice's email
>>> contacts["Alice"]["email"] = "alice.new@example.com"

>>> # Add new contact
>>> contacts["Charlie"] = {
...     "phone": "555-555-5555",
...     "email": "charlie@example.com"
... }

>>> # Delete existing contact
>>> del contacts["Bob"]

>>> contacts
{
    'Alice': {'phone': '123-456-7890', 'email': 'alice.new@example.com'},
    'Charlie': {'phone': '555-555-5555', 'email': 'charlie@example.com'}
}

In this example, the dictionary helps efficiently manage and update contact information by leveraging key-value pairs.

Tutorial

Dictionaries in Python

Learn how dictionaries in Python work: create and modify key-value pairs using dict literals, the dict() constructor, built-in methods, and operators.

basics python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Feb. 9, 2026 • Reviewed by Brenda Weleschuk and Dan Bader