How to Create a Dictionary from a List in Python

To create a dictionary from a list in Python, match the method to what the list holds: dict(pairs) for a list of key-value pairs, dict.fromkeys(keys) for a list of keys,:

dict([("NY", "New York"), ("TX", "Texas")])  # {'NY': 'New York', 'TX': 'Texas'}
dict.fromkeys(["Emily", "James"], 0.00)        # {'Emily': 0.0, 'James': 0.0}

I pasted every output below from real runs on Python 3.12.5, so you can trust them.

In this tutorial, we’ll go through each list shape, plus two traps: a shared default list and vanishing duplicate keys.

Create a dictionary from a list of tuples or pairs with dict()

When each item is already a two-element tuple or list, pass the whole list to dict(). Item one becomes the key, item two the value:

pairs = [("NY", "New York"), ("TX", "Texas"), ("CA", "California")]
states = dict(pairs)
print(states)

rows = [["SKU-101", 19.99], ["SKU-102", 4.50]]   # inner lists work too
prices = dict(rows)
print(prices)

bad = [("NY", "New York"), ("TX",)]             # one item is missing its value
try:
    dict(bad)
except ValueError as e:
    print("ValueError:", e)

Output:

{'NY': 'New York', 'TX': 'Texas', 'CA': 'California'}
{'SKU-101': 19.99, 'SKU-102': 4.5}
ValueError: dictionary update sequence element #1 has length 1; 2 is required
Command Prompt output of Python dict() building dictionaries from a list of tuples and a list of lists, plus the ValueError for a one-item tuple
Tuples and inner lists both work; a lone item raises a ValueError.

The error names the broken element. If your pairs live in a tuple of tuples instead, my post on converting a tuple to a dictionary in Python covers that input.

Create a dictionary from a list of keys with dict.fromkeys()

If the list only holds keys, dict.fromkeys() gives every key the same starting value. Watch out for mutable defaults:

customers = ["Emily", "James", "Olivia"]

balances = dict.fromkeys(customers, 0.00)
print(balances)

orders = dict.fromkeys(customers, [])      # trap: ONE list shared by every key
orders["Emily"].append("2026-03-14")
print(orders)

orders = {name: [] for name in customers}  # fix: a new list per key
orders["Emily"].append("2026-03-14")
print(orders)

Output:

{'Emily': 0.0, 'James': 0.0, 'Olivia': 0.0}
{'Emily': ['2026-03-14'], 'James': ['2026-03-14'], 'Olivia': ['2026-03-14']}
{'Emily': ['2026-03-14'], 'James': [], 'Olivia': []}
Command Prompt output of Python dict.fromkeys on a customer list, showing one shared list receiving the same date for every customer
One append to Emily shows up under all three customers until each key gets its own list.

Use fromkeys() for numbers, strings and None, and a comprehension for lists or dicts. More starting-value patterns are in initializing a Python dictionary with keys and values.

Build dictionary values from list items with a comprehension

A dict comprehension runs an expression for each item, so values come from the items. Here each city maps to its length:

cities = ["Boston", "Denver", "Austin", "Seattle"]

lengths = {city: len(city) for city in cities}
print(lengths)

Output:

{'Boston': 6, 'Denver': 6, 'Austin': 6, 'Seattle': 7}

Comprehensions get their own deep dive in Python dictionary comprehension. When the logic needs several lines, a plain loop reads better; see creating a dictionary in Python using a for loop.

How do I convert a flat list of alternating keys and values?

Form or CSV data often arrives as [key, value, key, value]. Slice out the even and odd positions and zip them back together:

flat = ["Jan", 1250.00, "Feb", 1480.50, "Mar", 1399.99]

sales = dict(zip(flat[::2], flat[1::2]))   # even items = keys, odd items = values
print(sales)

Output:

{'Jan': 1250.0, 'Feb': 1480.5, 'Mar': 1399.99}
Command Prompt output of Python zip with list slicing turning a flat list of months and sales into a dictionary and printing the quarter total
Even positions become the months and odd positions the sales amounts.

It’s a neat fix you’ll reuse. If your keys and values are already in separate lists, follow creating a dictionary from two lists in Python instead.

Key a list of dictionaries by ID, and watch for duplicate keys

Pick a unique field to turn records into a lookup table. A repeated key keeps only its last value:

employees = [
    {"id": 101, "name": "Megan Bowen", "dept": "Sales"},
    {"id": 102, "name": "Alex Wilber", "dept": "IT"},
    {"id": 103, "name": "Adele Vance", "dept": "Sales"},
]
by_id = {e["id"]: e for e in employees}
print(by_id[102]["name"], "works in", by_id[102]["dept"])

pairs = [("Sales", "Megan"), ("IT", "Alex"), ("Sales", "Adele")]
d = dict(pairs)
print(d)
print(len(pairs), "pairs ->", len(d), "keys")   # the later "Sales" value wins

Output:

Alex Wilber works in IT
{'Sales': 'Adele', 'IT': 'Alex'}
3 pairs -> 2 keys
Command Prompt output of Python indexing a list of employee dictionaries by id and dict() dropping a duplicate Sales key
Three pairs produce two keys: Adele replaces Megan under Sales without any warning.

Compare len() of the list and the dictionary whenever duplicates are possible; counting the keys in a Python dictionary shows the options.

Which list-to-dictionary method should you use?

List holdsUseResult
Pairsdict(pairs)First item = key
Keys onlydict.fromkeys(keys, v)Same value for every key
Items to transform{x: f(x) for x in lst}Computed values
Alternatingdict(zip(lst[::2], lst[1::2]))Even = key, odd = value
Dictionaries{r["id"]: r for r in lst}Lookup by ID

Good next reads once your dictionary exists:

Frequently asked questions

How do I create a dictionary from a list in Python?

Use dict(pairs) for a list of key-value pairs, dict.fromkeys(keys) for a list of keys,.

How do I convert a list to a dictionary with index keys?

Use dict(enumerate(items)), or dict(enumerate(items, start=1)) to start at 1.

Why does dict.fromkeys() with [] share one list?

The default is created once and reused. Use {k: [] for k in keys} instead.

What happens to duplicate keys when converting a list?

The last value wins; earlier ones are dropped silently.

Why do I get ‘dictionary update sequence element has length 1’?

An item in the list isn’t a pair of two values.

Where is the dict constructor documented?

See dict in the Python docs.