Python Dictionary: Creating a Dictionary

A dictionary in Python stores data as key-value pairs. This page shows the main ways to create a dictionary, the basic syntax, and a few common beginner mistakes to avoid.

If you are new to dictionaries, you may also want to read Python dictionaries explained or what is a dictionary in Python.

Quick example #

student = {
    "name": "Ana",
    "age": 20,
    "is_active": True
}

print(student)

Output:

{'name': 'Ana', 'age': 20, 'is_active': True}

Use curly braces with key-value pairs separated by commas. Keys and values are joined with a colon.

What this page covers #

  • What a dictionary is
  • The basic syntax for creating one
  • Common ways to make a dictionary
  • Beginner mistakes to avoid

What a dictionary is #

A dictionary stores data as key-value pairs.

  • A key is the label
  • A value is the data connected to that label

For example, in this dictionary:

student = {
    "name": "Ana"
}
  • "name" is the key
  • "Ana" is the value

Dictionaries are useful when your data has labels, such as:

  • name
  • age
  • email
  • country

Create a dictionary with curly braces #

This is the most common way to create a dictionary.

Use {} and write each item as key: value.

student = {
    "name": "Ana",
    "age": 20,
    "course": "Python"
}

print(student)

Output:

{'name': 'Ana', 'age': 20, 'course': 'Python'}

Syntax rules #

  • Use {} to start and end the dictionary
  • Write each item as key: value
  • Separate items with commas
  • String keys are usually written in quotes

Example:

book = {
    "title": "Python Basics",
    "pages": 150,
    "available": True
}

print(book)

This style is best when you already know the keys and values you want to add.

Create an empty dictionary #

Sometimes you want to start with an empty dictionary and add items later.

You can create an empty dictionary in two ways:

data = {}
print(data)
data = dict()
print(data)

Output:

{}

Both are correct.

Important beginner note #

{} creates an empty dictionary, not an empty set.

If you want an empty set, you must use set().

my_dict = {}
my_set = set()

print(type(my_dict))
print(type(my_set))

Output:

<class 'dict'>
<class 'set'>

Create a dictionary with dict() #

You can also use dict() to create a dictionary.

One simple way is with keyword arguments:

student = dict(name="Ana", age=20, active=True)
print(student)

Output:

{'name': 'Ana', 'age': 20, 'active': True}

This can be easy to read for small dictionaries.

Important rule for dict() #

When using keyword arguments:

  • keys must be valid Python names
  • you do not put quotes around the keys

This works:

user = dict(name="Sam", age=25)
print(user)

This does not work as keyword syntax:

# dict(first-name="Sam")

That key is not a valid Python name because it contains a hyphen.

If your keys are not valid Python names, use curly braces instead:

user = {
    "first-name": "Sam"
}

print(user)

Create a dictionary from pairs #

dict() can also build a dictionary from key-value pairs.

This is useful when your data already exists as pairs, such as a list of tuples.

pairs = [("name", "Ana"), ("age", 20), ("city", "Lima")]

student = dict(pairs)

print(student)

Output:

{'name': 'Ana', 'age': 20, 'city': 'Lima'}

You can also use tuples directly:

student = dict((("name", "Ana"), ("age", 20)))
print(student)

This approach is helpful when you receive data in pair form and want to turn it into a dictionary.

Rules beginners should know #

Here are a few important dictionary rules:

  • Keys must be unique
  • If the same key appears more than once, the last value replaces earlier ones
  • Values can repeat
  • Keys are often strings, but other immutable types can also be keys

Duplicate key example #

data = {
    "name": "Ana",
    "name": "Maria"
}

print(data)

Output:

{'name': 'Maria'}

Only the last value is kept.

Keys can be numbers too #

scores = {
    1: "low",
    2: "medium",
    3: "high"
}

print(scores)

Mutable types cannot be keys #

A list cannot be a dictionary key because lists are mutable.

This will cause an error:

# bad = {
#     [1, 2]: "numbers"
# }

But a tuple can be used as a key because tuples are immutable:

good = {
    (1, 2): "numbers"
}

print(good)

When to use a dictionary #

Use a dictionary when each value has a label.

Dictionaries are a good choice for:

  • user data
  • settings
  • counters
  • lookups

Example:

user = {
    "username": "ana123",
    "email": "ana@example.com",
    "logged_in": True
}

print(user["email"])

If you want to learn how to read values from a dictionary, see how to access values in a dictionary in Python.

A list is usually a better choice when:

  • labels are not needed
  • you mostly care about position
  • order matters more than named fields

Common mistakes #

Beginners often make these mistakes when creating dictionaries:

  • Using commas instead of colons between keys and values
  • Forgetting quotes around string keys when needed
  • Repeating the same key and expecting both values to stay
  • Using a mutable type like a list as a dictionary key
  • Mixing up {} for dictionaries and sets

Mistake: using a comma instead of a colon #

Wrong:

# student = {"name", "Ana"}

Correct:

student = {"name": "Ana"}
print(student)

Mistake: forgetting quotes around a string key #

Wrong:

# student = {name: "Ana"}

This only works if name is already a variable. If you want the key to be the text "name", use quotes.

Correct:

student = {"name": "Ana"}
print(student)

Mistake: expecting duplicate keys to stay #

data = {"age": 20, "age": 30}
print(data)

Output:

{'age': 30}

Helpful checks while learning #

These quick commands can help you inspect a dictionary:

my_dict = {"name": "Ana", "age": 20}

print(my_dict)
print(type(my_dict))
print(my_dict.keys())
print(my_dict.values())
print(len(my_dict))

Example output:

{'name': 'Ana', 'age': 20}
<class 'dict'>
dict_keys(['name', 'age'])
dict_values(['Ana', 20])
2

If you want to learn these methods in more detail, see Python dictionary keys(), Python dictionary values(), and Python dictionary items().

FAQ #

How do I create an empty dictionary in Python? #

Use {} or dict(). Both create an empty dictionary.

What is the easiest way to create a dictionary? #

The easiest way is usually curly braces with key-value pairs, like {"name": "Ana", "age": 20}.

Can dictionary keys be numbers? #

Yes. Keys can be numbers, strings, tuples, and other immutable types.

Can a dictionary have duplicate keys? #

A dictionary can be written with duplicate keys, but only the last value for that key is kept.

What is the difference between {} and dict()? #

{} is the most common literal syntax. dict() is a constructor and is useful for empty dictionaries, keyword arguments, or key-value pair data.

See also #

Next step: learn how to access, add, and safely read dictionary values.

Press Esc to close