5 Best Ways to Convert a Python List to a List of Dictionaries

πŸ’‘ Problem Formulation:

Often in Python programming, there is a requirement to convert a list of items into a list of dictionaries to make it easier to work with JSON, databases, or simply to manipulate the data in a structured key-value format. For instance, transforming ['apple', 'banana', 'cherry'] into [{'fruit': 'apple'}, {'fruit': 'banana'}, {'fruit': 'cherry'}].

Method 1: Using a for-loop

This method employs a traditional for-loop to iterate over each item in the list and creates a dictionary with specified keys, adding that dictionary to a new list. It offers fine control over dictionary keys and values during transformation.

Here’s an example:

fruits = ['apple', 'banana', 'cherry']
fruits_dict_list = []
for fruit in fruits:
    fruits_dict_list.append({'fruit': fruit})

Output:

[{'fruit': 'apple'}, {'fruit': 'banana'}, {'fruit': 'cherry'}]

This snippet creates an empty list fruits_dict_list and appends a new dictionary for each fruit in the list fruits, where the key is ‘fruit’ and the value is the actual fruit string.

Method 2: Using List Comprehension

List comprehension is a concise way to create a new list in Python. This method takes advantage of list comprehension to create a list of dictionaries in a single line of code.

Here’s an example:

fruits = ['apple', 'banana', 'cherry']
fruits_dict_list = [{'fruit': fruit} for fruit in fruits]

Output:

[{'fruit': 'apple'}, {'fruit': 'banana'}, {'fruit': 'cherry'}]

The above line of code uses list comprehension to iterate over each item in the list fruits and generates a dictionary with the key ‘fruit’ and the correspondent fruit as the value.

Method 3: Using the map() Function

The map() function is often used to apply a function to each item in an iterable. This method applies a lambda function to generate the desired list of dictionaries.

Here’s an example:

fruits = ['apple', 'banana', 'cherry']
fruits_dict_list = list(map(lambda fruit: {'fruit': fruit}, fruits))

Output:

[{'fruit': 'apple'}, {'fruit': 'banana'}, {'fruit': 'cherry'}]

The code uses map() with a lambda function that creates a dictionary for each element in the list fruits. The resulting map object is then converted into a list.

Method 4: Using dict() and zip()

If a list is composed of pairs or tuples, the zip() function in conjunction with dict() can be used to convert the list into a list of dictionaries; each tuple or pair forms a key-value pair of the dictionary.

Here’s an example:

pairs = [('fruit', 'apple'), ('fruit', 'banana'), ('fruit', 'cherry')]
fruits_dict_list = [dict([pair]) for pair in pairs]

Output:

[{'fruit': 'apple'}, {'fruit': 'banana'}, {'fruit': 'cherry'}]

The code snippet uses a list comprehension along with the dict() function to turn each tuple in the list pairs into a dictionary.

Bonus One-Liner Method 5: Using a Dictionary Comprehension

Similar to list comprehensions, dictionary comprehensions provide a succinct way to generate dictionaries. This can be used in a one-liner to convert a list of keys, and a single value, to a list of dictionaries.

Here’s an example:

keys = ['name', 'type', 'color']
value = 'fruit'
dicts_list = [{key: value} for key in keys]

Output:

[{'name': 'fruit'}, {'type': 'fruit'}, {'color': 'fruit'}]

This one-liner uses dictionary comprehension to create a list of dictionaries, where each dictionary is composed of one key from the keys list and a single shared value.

Summary/Discussion

  • Method 1: For-loop. Good for complex transformations. Can be more verbose than other methods.
  • Method 2: List Comprehension. Concise and Pythonic; great for simple transformations. Readability might reduce for more complex cases.
  • Method 3: map() Function. Functional programming style; clean, but less Pythonic than list comprehension. Requires conversion to list.
  • Method 4: dict() and zip(). Useful for converting a list of pairs/tuples. Less intuitive for those who are new to Python.
  • Method 5: Dictionary Comprehension. One-liner and concise for creating lists of similar dictionaries; lacks the flexibility for diverse values.