How to convert a list of strings to list of dictionaries in python?

Juan Antonio Clavero Garcia

I have a string with format:

["('colA':'datA1', 'colB':'datB1', 'colC':'datC1')",
 "('colA':'datA2', 'colB':'datB2', 'colC':'datC2')",
 ..........
 "('colA':'datAn', 'colB':'datBn', 'colC':'datCn')]

And i need to obtain a dict:

[{'colA': 'datA1', 'colB': 'datB1', 'colC': 'datC1'},
 {'colA': 'datA2', 'colB': 'datB2', 'colC': 'datC2'},
 ..........
 {'colA': 'datAn', 'colB': 'datBn', 'colC': 'datCn'}]

If possible I need to do it with Python's own functions or at least without using loops, but I do not know if this is possible.

TigerhawkT3

As @nexus66 suggested, here's a more streamlined version of that answer:

l = ["('colA':'datA1', 'colB':'datB1', 'colC':'datC1')",
 "('colA':'datA2', 'colB':'datB2', 'colC':'datC2')",
 "('colA':'datAn', 'colB':'datBn', 'colC':'datCn')"]
result = [dict(
               item.replace("'", '').split(':')
               for item in s[1:-1].split(', ')
               )
          for s in l]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert a list of dictionaries to JSON in Python / Django?

How to convert a list of tuples to a dictionary of dictionaries in Python?

How to convert selenium webelelements to list of strings in python

Turn list of strings into list of dictionaries python

Convert list of dictionaries into dataframe python

Python: Convert list of dictionaries to list of lists

convert string to list of dictionaries in python

Convert csv into list of dictionaries in python

Convert list of dictionaries to EXCEL with Python

How to quickly convert from items in a list of lists to list of dictionaries in python?

How convert list of dictionaries to a dictionary of dictionaries in Python

Convert strings to list Python

How to convert a list of dictionaries into a csv?

how to convert multiple strings into list using python

How to convert string containing list of dictionaries to python (object) LIST of DICT

Convert a list to list of dictionaries in Python 3

Python - How to convert a list of dictionaries with tree items?

How to convert CSV file into a list of dictionaries in python

How to convert a list of lists with tuples into a list of dictionaries?

Convert XML to List of Dictionaries in python

Convert string to list within list of dictionaries in python

How to convert the list into a list of dictionaries?

Ansible: How to convert list of strings to a list of dictionaries

How do I convert a list of strings to a list of dictionaries?

How to convert list of dictionaries to list of tuples?

Python: convert list of dictionaries into dataframe

How can I convert a list of strings to list of dictionaries without using zip function?

Python dict comprehension convert list of tuples of dictionaries and integers into list of dictionaries

Convert list to list of dictionaries: Python