I have an array of dict like:
[{'id': 1, 'tags': ['tag_a', 'tag_b']}, {'id': 2, 'tags': ['tag_c', 'tag_b']}]
and I would like to convert this array into a new array based on ticket_id and each tag value should be an object in the array.
I almost did it using the following function:
raw = [{'id': 1, 'tags': ['tag_a', 'tag_b']}, {'id': 2, 'tags': ['tag_c', 'tag_b']}]
def transform(ticket):
result = []
for tag in ticket.get('tags', []):
result.append({'ticket_id': ticket.get('id'), 'tag': tag, 'sync': '123'})
return result
print(list(map(transform, raw)))
But it's returning something like [[{...}], [{...}]] and the syntax looks strange.
What is the right approach to return something like:
[{'ticket_id': 1, 'tag': 'tag_a', 'sync': '123'}, {'ticket_id': 1, 'tag': 'tag_b', 'sync': '123'}, ...]