How to Create a Dictionary in Python (Literals & Constructors)

Learn how to create a dictionary in Python. Understand literal syntax, dict constructor, dict comprehensions, and keys-values initializers.

Try Code in Editor

Explanation

Dictionaries are one of the most powerful and heavily utilized built-in data types in Python. A dictionary is a mutable, ordered (since Python 3.7) collection of key-value pairs, where each key must be unique and hashable (e.g., strings, numbers, or tuples). Dictionaries allow you to perform lookup operations with highly optimized O(1) average-case time complexity.

The most common and direct way to create a dictionary is using curly braces `{}` with key-value pairs separated by colons. For instance, `my_dict = {"name": "Alice", "age": 30}` creates a populated dictionary. To create an empty dictionary, you can use empty curly braces `{}` or the `dict()` constructor.

Python also offers other advanced methods to create dictionaries dynamically. The `dict()` constructor can build dictionaries from lists of key-value tuples or keyword arguments. Additionally, dictionary comprehensions provide a powerful way to initialize a dictionary from an existing iterable, applying transformations to keys or values inline.

Step-by-Step Implementation

  1. 1

    Use curly braces {} with key: value pairs to create a dictionary literal.

  2. 2

    Call dict() to create an empty dictionary or construct one using keyword arguments.

  3. 3

    Pass a list of key-value tuples to dict() to transform relational arrays into a dictionary object.

Code Example

This script demonstrates multiple ways to initialize and create dictionaries in Python.

create_dict.py
Try in Editor
# 1. Dictionary literal syntax (most common)
user = {"name": "Alice", "age": 28, "verified": True}
print("Literal dict:", user)

# 2. Using the dict() constructor with keyword arguments
config = dict(host="localhost", port=8080, debug=True)
print("Constructor dict:", config)

# 3. Creating dictionary from key-value pairs (tuples)
pairs = [("id", 101), ("status", "active")]
status_dict = dict(pairs)
print("From tuples:", status_dict)

# 4. Dictionary comprehension (dynamic creation)
squares = {x: x**2 for x in range(1, 5)}
print("Comprehension:", squares)
Terminal Output
Literal dict: {'name': 'Alice', 'age': 28, 'verified': True}
Constructor dict: {'host': 'localhost', 'port': 8080, 'debug': True}
From tuples: {'id': 101, 'status': 'active'}
Comprehension: {1: 1, 2: 4, 3: 9, 4: 16}

Frequently Asked Questions

Can list or dictionary items be used as dictionary keys?

No, dictionary keys must be hashable (immutable). Since lists and dictionaries are mutable, they cannot be keys. Tuples, strings, and integers are valid keys.

How can I create a dictionary with default values for a set of keys?

You can use dict.fromkeys(keys_list, default_value) to construct a dictionary where all keys share the same default value.

Related How-To Guides

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.