In Python, the original dict list is as follows:
orig = [{'team': 'team1', 'other': 'blah', 'abbrev': 't1'},
{'team': 'team2', 'other': 'blah', 'abbrev': 't2'},
{'team': 'team3', 'other': 'blah', 'abbrev': 't3'},
{'team': 'team1', 'other': 'blah', 'abbrev': 't1'},
{'team': 'team3', 'other': 'blah', 'abbrev': 't3'}]
Need to get a new dict list of just team and abbrev but of distinct teams into a list like this:
new = [{'team': 'team1', 'abbrev': 't1'},
{'team': 'team2', 'abbrev': 't2'},
{'team': 'team3', 'abbrev': 't3'}]
dict keys are unique, which you can exploit:
teamdict = dict([(data['team'], data) for data in t])
new = [{'team': team, 'abbrev': data['abbrev']} for (team, data) in teamdict.items()]
Can't help to suggest that a dict might be your data structure of choice to begin with.
Oh, I don't know how dict()
reacts to the fact that there are several identical keys in the list. You may have to construct the dictionary less pythonesque in that case:
teamdict={}
for data in t:
teamdict[data['team']] = data
implicitly overriding double entries
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.