5 Best Ways to Convert a Python List of Strings to a List of Dicts

πŸ’‘ Problem Formulation:

When working with data in Python, we often need to convert a list of strings into a more structured format like a list of dictionaries. This enables easier manipulation and access to individual data components. For example, consider an input ['name: John', 'age: 22', 'city: New York'] which we want to transform into [{'name': 'John'}, {'age': '22'}, {'city': 'New York'}]. This article explores various methods to achieve this conversion.

Method 1: Using a Loop and Split

This method involves iterating over the list and splitting each string into a key and value pair which is then used to create a dictionary. This simple approach works well when the strings are uniformly formatted.

β™₯️ Info: Are you AI curious but you still have to create real impactful projects? Join our official AI builder club on Skool (only $5): SHIP! - One Project Per Month

Here’s an example:

input_list = ['name: John', 'age: 22', 'city: New York']
dict_list = []

for item in input_list:
    key, value = item.split(': ')
    dict_list.append({key: value})

Output: [{'name': 'John'}, {'age': '22'}, {'city': 'New York'}]

The code snippet takes each string from the input list, splits it by the colon followed by a space, and then creates a single-key dictionary from the resulting two items. Each of these dictionaries is appended to a new list, producing a list of dictionaries.

Method 2: Using List Comprehension with Split

List comprehension in Python provides a concise way to create lists. It can be used in this case to shorten the loop syntax from Method 1, making the code more Pythonic and often easier to read.

Here’s an example:

input_list = ['name: John', 'age: 22', 'city: New York']
dict_list = [{item.split(': ')[0]: item.split(': ')[1]} for item in input_list]

Output: [{'name': 'John'}, {'age': '22'}, {'city': 'New York'}]

The list comprehension iterates through each string in the input list, splits it into key and value, and directly creates a dictionary from these parts. The result is a more compact version of Method 1.

Method 3: Using map and lambda Functions

The map() function can apply a lambda (anonymous) function to each element of a list. This is useful for transforming a list of strings into a list of dictionaries without explicitly writing a loop.

Here’s an example:

input_list = ['name: John', 'age: 22', 'city: New York']
dict_list = list(map(lambda item: {item.split(': ')[0]: item.split(': ')[1]}, input_list))

Output: [{'name': 'John'}, {'age': '22'}, {'city': 'New York'}]

In this snippet, the map() function takes a lambda function that generates a dictionary from a split string and applies it to each element of input_list. The result is then converted back into a list.

Method 4: Using Regular Expressions

When the strings have more complex patterns, regular expressions can be utilized to extract the key-value pairs. This method is particularly versatile and powerful when dealing with varied input formats.

Here’s an example:

import re

input_list = ['name: John', 'age: 22', 'city: New York']
pattern = r'(\w+): (\w.+)'
dict_list = [{match.group(1): match.group(2)} for item in input_list if (match := re.search(pattern, item))]

Output: [{'name': 'John'}, {'age': '22'}, {'city': 'New York'}]

The regular expression (\w+): (\w.+) matches one or more word characters followed by a colon and a space, and then at least one more character. The list comprehension with a conditional assignment in the if statement ensures that only matching strings are processed.

Bonus One-Liner Method 5: Using json.loads with String Replacement

For strings already formatted similarly to JSON, the json.loads() method, combined with string replacement to conform to JSON syntax, can be an efficient solution.

Here’s an example:

import json

input_list = ['{"name": "John"}', '{"age": "22"}', '{"city": "New York"}']
dict_list = [json.loads(item.replace("'", "\"")) for item in input_list]

Output: [{'name': 'John'}, {'age': '22'}, {'city': 'New York'}]

Assuming that the input strings are single dictionaries in JSON format with single quotes, json.loads() is used to parse each string into a dictionary. The string replacement ensures the JSON format is correct.

Summary/Discussion

  • Method 1: Loop and Split. Straightforward. May not be the most concise solution.
  • Method 2: List Comprehension with Split. More Pythonic. Requires two split operations per loop iteration.
  • Method 3: Map and Lambda Functions. Functional programming style. Less readable for Python beginners.
  • Method 4: Regular Expressions. Highly customizable for complex patterns. Overkill for simple string structures.
  • Bonus Method 5: json.loads with String Replacement. Quick for JSON-like strings. Requires correct formatting upfront.