← Back to Blog

From Empty to Full: How to Create Dicts in Python

The problem...

You know what a dict is. Key, value, instant lookup.

But there's more than one way to build one — and each fits a different situation.

Knowing which to reach for saves time and keeps your code clean.

The most common way

Curly braces, keys and values separated by colons, entries separated by commas.

soldier = {"name": "Raven", "score": 85, "active": True}
print(soldier)

Output → {'name': 'Raven', 'score': 85, 'active': True}

This is a dict literal. You know the keys and values upfront. You type them in. Done.

For readability, spread it across lines:

soldier = {
    "name":   "Raven",
    "score":  85,
    "active": True
}

Starting empty

Sometimes you don't have data yet. You need a dict that starts empty and gets filled later.

roster = {}

You can also use the built-in dict() function:

roster = dict()

Both create an empty dict. {} is more common. You'll see both.

Using dict() with keyword arguments

dict() lets you pass keys as keyword arguments — no quotes needed on the keys:

soldier = dict(name="Raven", score=85, active=True)
print(soldier)

Output → {'name': 'Raven', 'score': 85, 'active': True}

Cleaner to type, but only works when your keys are valid Python identifiers — no spaces, no hyphens, no numbers at the start.

Building from two lists

If you have a list of keys and a list of values, zip() pairs them up and dict() turns them into a dict:

names  = ["Raven", "Wolf", "Ghost"]
scores = [85, 74, 91]
result = dict(zip(names, scores))
print(result)

Output → {'Raven': 85, 'Wolf': 74, 'Ghost': 91}

One line. Two lists in. One dict out.

Adding keys one at a time

You can start empty and assign keys directly:

roster = {}
roster["Raven"] = 85
roster["Wolf"]  = 74
roster["Ghost"] = 91
print(roster)

Output → {'Raven': 85, 'Wolf': 74, 'Ghost': 91}

If the key already exists, this updates its value. If it doesn't, it creates it.

What's really happening

All of these produce the same structure — a dict with keys and values.

The method you choose depends on where your data comes from: typed upfront, built from lists, or assembled piece by piece.

Heads up!

  • {} creates an empty dict — not an empty set (that's set())
  • dict() with keyword arguments only works for simple string keys
  • dict(zip()) stops at the shorter list — same as zip() alone
  • Assigning to a key that exists overwrites the value — no error, no warning

The mindset shift

Stop thinking: "I'll figure out how to build the dict when I need one."

Start thinking: "Where is my data coming from — and which creation method fits that?"

What you should understand now

  • A dict literal uses curly braces with key-value pairs inside
  • An empty dict is created with {} or dict()
  • dict(key=value) works for simple string keys without quotes
  • dict(zip(keys, values)) builds a dict from two lists
  • Assigning to a key creates it if it doesn't exist, updates it if it does
[ login to bookmark ] // copied! 18 views · 2 min
// resources
Code Example dict_creation.py
← prev The Dict: One Key, One Value, Zero Guessing next → How to Ask a Dict for What You Need
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.