What I'm trying to do is split a list of dictionaries if a field has n values separated by commas, into a list of n dictionaries where all other fields are same but the field with multiple values now has unique values.
Here's the output:
before[{'id': 1, 'time': '1,2,3'}, {'id': 2, 'time': '4,5'}, {'id': 3, 'time': '6'}, {'id': 4, 'time': '7,8,9,10'}]during['1', '2', '3']{'id': 1, 'time': '1'}{'id': 1, 'time': '2'}{'id': 1, 'time': '3'}['4', '5']{'id': 2, 'time': '4'}{'id': 2, 'time': '5'}['7', '8', '9', '10']{'id': 4, 'time': '7'}{'id': 4, 'time': '8'}{'id': 4, 'time': '9'}{'id': 4, 'time': '10'}after{'id': 1, 'time': '3'}{'id': 2, 'time': '5'}{'id': 3, 'time': '6'}{'id': 4, 'time': '10'}{'id': 1, 'time': '3'}{'id': 1, 'time': '3'}{'id': 2, 'time': '5'}{'id': 4, 'time': '10'}{'id': 4, 'time': '10'}{'id': 4, 'time': '10'}
Why is this happening?How can I make the code better?Thanks in advance.
Here's my code
meta = [{"id":1,"time":"1,2,3"},{"id":2,"time":"4,5"},{"id":3,"time":"6"},{"id":4,"time":"7,8,9,10"}]print("before")print(meta)meta_copy = meta.copy()placeholder = meta.copy()print("during")for i in range(len(meta)): if "," in meta[i]["time"]: sub = meta[i]["time"].split(",") print(sub) for j in range(len(sub)): if j == 0: meta_copy[i]["time"] = sub[0] print(meta_copy[i]) else: placeholder[i]["time"]=sub[j] #print(placeholder[i]["time"]) meta_copy.append(placeholder[i]) print( meta_copy[-1])print("after")for l in range(len(meta_copy)): print(meta_copy[l])