# Python 3: Creating list of multiple dictionaries have same keys but different values coming from multiple lists
import pprint
meta_names = {
'object_name': ['file1234', 'file5671'],
'object_size_KB': ['9347627', '9406875'],
'object_text': ['Bob', 'Ross']
}
def parse_response(meta_names):
"""
input: response: api xml response text from lxml xpath
input: meta_names: key names used to generate dictionary per object
return: list of objects dictionary
"""
# List comprehensions
to_dict = lambda l: [{key:val for key,val in pairs} for pairs in l]
objs = list(zip(*list([[key,val] for val in vals] for key,vals in meta_names.items())))
pprint.pprint(to_dict(objs))
print('\n')
# for-loop style
objs3 = []
for key,vals in meta_names.items():
temp = []
for val in vals:
temp.append([key,val])
objs3.append(temp)
pprint.pprint(to_dict(list(zip(*objs3))))
parse_response(meta_names)