Answer the question
In order to leave comments, you need to log in
How to transfer data from one dictionary to another dictionary or array?
There is the following list with a dictionary:
odds = [
{'Date': '2021-11-20T12:30:00Z', 'Odds': 4.55, 'Team': 'Leicester City'},
{'Date': '2021-11-28T14:00:00Z', 'Odds': 1.47, 'Team': 'Leicester City'},
{'Date': '2021-11-20T15:00:00Z', 'Odds': 2.75, 'Team': 'Burnley'},
{'Date': '2021-11-28T14:00:00Z', 'Odds': 3.55, 'Team': 'Burnley'},
]
{'Date': '2021-11-20T12:30:00Z', 'Odds1': 4.55, 'Odds2': 1.47, 'Team': 'Leicester City'},
Answer the question
In order to leave comments, you need to log in
defaultdict
Try this:
temp_data = defaultdict(list)
for data in odds:
temp_data[data['Team']].append((data['Odds'], data['Date']))
temp_data = {key: zip(*value) for key, value in temp_data.items()}
new_data = [dict(Team=team, Odds=odds, Date=date) for team, (odds, date) in temp_data.items()]
odds = [
{'Date': '2021-11-20T12:30:00Z', 'Odds': 4.55, 'Team': 'Leicester City'},
{'Date': '2021-11-28T14:00:00Z', 'Odds': 1.47, 'Team': 'Leicester City'},
{'Date': '2021-11-20T15:00:00Z', 'Odds': 2.75, 'Team': 'Burnley'},
{'Date': '2021-11-28T14:00:00Z', 'Odds': 3.55, 'Team': 'Burnley'},
]
new=[]
for i in odds:
new.append(dict())
for k,v in i.items():
new[-1][k]=v
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question