How To Populate A Dictionary Using For Loop in Python – With Examples

Python dictionary allows you to store value in a key-value pair.

You can populate a dictionary using for loop in Python using yourdict = {k:v for k,v in zip(keys, values)} statement.

Basic Example

keys = ["One", "Two", "Three", "Four"]

values = [1,2,3,4]

yourdict = {k:v for k,v in zip(keys, values)}

yourdict

Output

    {'One': 1, 'Two': 2, 'Three': 3, 'Four': 4}

There are different methods to populate a dictionary using for loop. Each method is applicable based on the list of values you have. Let us look at each method in detail.

To append values to an existing dictionary, read: How to Add Keys to Dictionary

Using Zip And For Loop

You can populate a dictionary using the dictionary comprehension and the zip() method.

  • The zip() method takes one value from each list passed and returns it to the for loop during each iteration.
  • The returned values are created as a key and value, and finally, it is assigned to the dictionary object.

Use this method when you want to create a dictionary from two available lists of values.

Code

keys = ["One", "Two", "Three", "Four"]

values = [1,2,3,4]

yourdict = {k:v for k,v in zip(keys, values)}

yourdict

Output

    {'One': 1, 'Two': 2, 'Three': 3, 'Four': 4}

Using Dictionary Constructor & Zip

This section teaches you how to use the dictionary constructor with the zip() method instead of the dictionary comprehension.

  • Pass the zip method output to the dictionary constructor, as demonstrated in the following code.
keys = ["One", "Two", "Three", "Four"]

values = [1,2,3,4]

yourdict = dict(zip(keys, values))

yourdict

Output

    {'One': 1, 'Two': 2, 'Three': 3, 'Four': 4}

Populate Dictionary From List And For Loop

This section teaches you how to use the for loop alone to create a dictionary.

  • Iterate over a list of values that you want to be considered as a key
  • During each iteration, use an iteration variable and populate the dictionary key-value pair.
  • Iteration variable is necessary because the keys might not always be the integer value that can be used to access the list using that index.

This method is more verbose. It is recommended to use dictionary comprehension when you want to create a dictionary with two lists of values.

Code

yourdict = {}

keys = ["One", "Two", "Three", "Four"]

values = [1,2,3,4]

i=0;

for key in keys:

        yourdict[key] = values[i]
        i=i+1

print(yourdict)

Output

    {'One': 1, 'Two': 2, 'Three': 3, 'Four': 4}

Using Enumerate

Enumerate returns an enumerable object of tuples, and each tuple contains an index and an item from the iterable.

  • Pass the enumerable object of tuples to the list() constructor and pass the list to the dict() constructor.
  • You’ll have the dictionary populated with the index of the list as keys and list values as values for the dictionary keys

Code

values = [1,2,3,4]

yourdict = dict(list(enumerate(values)))

yourdict

Output

{0: 1, 1: 2, 2: 3, 3: 4}

This is how you can store the values from a list in a dictionary.

Additional Resources

Leave a Comment