Let's say that your list of dictionaries is called data.
Then you can convert it with the following code:
newdata = [{dic['answer_type']: dic['answer__count']} for dic in data]
If you don't understand this code well, take a look at Python comprehensions
Answer from nyr1o on Stack OverflowLet's say that your list of dictionaries is called data.
Then you can convert it with the following code:
newdata = [{dic['answer_type']: dic['answer__count']} for dic in data]
If you don't understand this code well, take a look at Python comprehensions
Assuming your data is stored in d, there are several ways to do this:
List Comprehension
For every iterable you can use a list comprehension, so iterate over the items with [it for it in iterable] (you can use list comprehensions in dictionaries, too).
In [6]: [{dic['answer_type']: dic['answer__count']} for dic in d]
Out[6]: [{'Yes': 5}, {'No': 3}, {'Maybe': 2}]
Functional Approach
If you are more the functional programming guy, consider using a map, which applies a function to each element of an iterable (this is my preferred approach, if I have to do this again and again with a list, I only have to define the function once then):
In [2]: map(lambda dic: {dic['answer_type']: dic['answer__count']}, d)
Out[2]: [{'Yes': 5}, {'No': 3}, {'Maybe': 2}]
For some people this is not as good to read as list comprehensions, for some it's better, depends on personal feelings. This will work with python2, in py3 it gives you an iterator, so you have to use list around the map to get the list.
Try this one please.
new_dict_list = [{'name': k, 'time': v} for k, v in dict.items()]
This is as simple as continuously making a dictionary with those values. We can use dict.items() to iterate through the items (key value pair).
new_dict_list = [
{'name': name, 'time': time}
for name, time in dict.items()
]
You probably want a single object like this:
sample_dict = [{"id":1, "count":10},{"id":2, "count":20},{"id":3, "count":30}]
out = { o['id']: o['count'] for o in sample_dict }
print(out) # {1: 10, 2: 20, 3: 30}
print(out[2]) # 20
Note that although the id and count values are in-order, this is not a requirement for this method. So long as the id values are unique, this will work.
sample_dict = [{"id":1, "count":10},
{"id":2, "count":20},
{"id":3, "count":30}]
output = []
for elem in sample_dict:
new_dict = {elem["id"]: elem["count"]}
output.append(new_dict)
Printing output will return
print(output)
[{1: 10}, {2: 20}, {3: 30}]
Without having to know the number of keys each dict has in advance, you can iterate through the list, split each string into a key and a value by ': ', appending a new dict to the list if the key is already in the last dict, and keep adding the value to the last dict by the key:
output = []
for key_value in lst:
key, value = key_value.split(': ', 1)
if not output or key in output[-1]:
output.append({})
output[-1][key] = value
so that given your sample list stored in lst, output would become:
[{'name': 'test1',
'email': '[email protected]',
'role': 'test',
'description': 'test'},
{'name': 'test2',
'email': '[email protected]',
'role': 'test2',
'description': 'test2'},
{'name': 'test3',
'email': '[email protected]',
'role': 'test3',
'description': 'test3'}]
I am assuming that your order is always the same, i.e., in groups of 4. The idea is to split the strings using : and then create key/value pairs and use nested for loops. The .strip() is to get rid of whitespace
lst = ['name: test1', 'email: [email protected]', 'role: test', 'description: test',
'name: test2', 'email: [email protected]', 'role: test2', 'description: test2',
'name: test3', 'email: [email protected]', 'role: test3', 'description: test3']
answer = []
for i in range(0, len(lst), 4):
dic = {}
for j in lst[i:i+4]:
dic[j.split(':')[0]] = j.split(':')[1].strip()
answer.append(dic)
# [{'name': 'test1', 'email': '[email protected]', 'role': 'test', 'description': 'test'},
# {'name': 'test2', 'email': '[email protected]', 'role': 'test2', 'description': 'test2'},
# {'name': 'test3', 'email': '[email protected]', 'role': 'test3', 'description': 'test3'}]
A list comprehension would look like
answer = [{j.split(':')[0]:j.split(':')[1].strip() for j in lst[i:i+4]} for i in range(0, len(lst), 4)]