Make a dictionary (dict) from separate lists of keys and values

Learn make a dictionary (dict) from separate lists of keys and values with practical examples, diagrams, and best practices. Covers python, list, dictionary development techniques with visual expla...

Crafting Dictionaries from Separate Key and Value Lists in Python

Hero image for Make a dictionary (dict) from separate lists of keys and values

Learn various Pythonic methods to efficiently combine two lists – one containing keys and another with corresponding values – into a single dictionary. This guide covers common scenarios and best practices.

Python dictionaries are powerful data structures that store data in key-value pairs. Often, you might find yourself with data organized into two separate lists: one holding the keys and another holding the values. The challenge then becomes how to elegantly and efficiently combine these into a dictionary. This article explores several common and Pythonic approaches to achieve this, catering to different scenarios and Python versions.

The zip() Function: The Most Pythonic Approach

The zip() function is arguably the most straightforward and Pythonic way to combine two lists into a dictionary. It takes two or more iterables and aggregates elements from each into tuples. When used with dict(), it directly constructs a dictionary from these key-value tuples.

keys = ['name', 'age', 'city']
values = ['Alice', 30, 'New York']

my_dict = dict(zip(keys, values))
print(my_dict)

Using zip() and dict() to create a dictionary.

Dictionary Comprehension: For More Control and Transformations

Dictionary comprehensions provide a concise way to create dictionaries. They are particularly useful when you need to apply transformations to either the keys or the values (or both) during the dictionary creation process. Similar to zip(), you can iterate over both lists simultaneously.

keys = ['item_1', 'item_2', 'item_3']
values = [100, 200, 300]

# Basic dictionary comprehension
my_dict_comp = {key: value for key, value in zip(keys, values)}
print(my_dict_comp)

# With transformation (e.g., converting keys to uppercase)
my_dict_transformed = {key.upper(): value * 2 for key, value in zip(keys, values)}
print(my_dict_transformed)

Creating a dictionary using dictionary comprehension, with and without transformations.

flowchart TD
    A[Start with Keys List] --> B[Start with Values List]
    B --> C{Are lists of equal length?}
    C -->|No| D[Warning: Data Loss Possible]
    C -->|Yes or Accept Loss| E[Zip Keys and Values]
    E --> F[Convert Zipped Tuples to Dictionary]
    F --> G[Resulting Dictionary]
    D --> G

Flowchart illustrating the process of combining keys and values into a dictionary.

Iterating and Assigning: A More Explicit Approach

For situations where you might need more granular control, or if you're working with older Python versions (though zip() and comprehensions are generally preferred), you can iterate through one list and assign key-value pairs to an empty dictionary. This often involves using range() and indexing, or enumerate().

keys = ['product_a', 'product_b', 'product_c']
values = [50.50, 75.25, 120.00]

# Using range and index
manual_dict = {}
for i in range(len(keys)):
    manual_dict[keys[i]] = values[i]
print(manual_dict)

# Using enumerate (similar to zip but iterates over one list with index)
enum_dict = {}
for i, key in enumerate(keys):
    enum_dict[key] = values[i]
print(enum_dict)

Manually building a dictionary using loops and indexing.