I have a list of dictionaries with identical keys. Furthermore, there are only two 'key: value' pairs per dictionary. I want to convert the list of dictionaries to a single dictionary with two keys, each representing a list value. Such that values of identical keys reside in one list.
I came up with the following approach, which by the way works perfectly fine. But I am hoping to know if there is any better practical approach.
The List of Dictionaries:
my_list = [{'old_price': None, 'price': '5,75'}, {'old_price': None, 'price': '5,90'}, {'old_price': None, 'price': '5,95'}, {'old_price': None, 'price': '10,15'}, {'old_price': None, 'price': '19,90'}, {'old_price': None, 'price': '34,90'}, {'old_price': None, 'price': '46,50'}, {'old_price': None, 'price': '24,90'}]
List of Dictionaries to Dictionary of Lists:
single_dict = {"old_price": [item['old_price'] for item in my_list], "price":[item['price'] for item in my_list]}
The Resulted Dictionary:
{'old_price': [None, None, None, None, None, None, None, None], 'price': ['5,75', '5,90', '5,95', '10,15', '19,90', '34,90', '46,50', '24,90']}