Skip to content Skip to sidebar Skip to footer

How To Create A List Based On Same Value Of A Dictionary Key

I am trying to join together dictionaries that contain the same date, and also create a list of the temperature values that these common dates have to then pull the max and min of

Solution 1:

Iterate over group to sort out mins and maxs to separate keys of the dictionary:

def get_temp(temp):
    return temp['date']

lst = []
for key, group in itertools.groupby(data, get_temp):
    groups = list(group)
    d = {}
    d['date'] = key
    d['temp_min'] = [x['temp_min'] for x ingroups]
    d['temp_max'] = [x['temp_max'] for x ingroups]
    lst.append(d)

print(lst)

Solution 2:

You can use defaultdicts to build the lists and then list comprehension to reconstruct the list of dictionaries:

from collections import defaultdict
mx = defaultdict(list)
mn = defaultdict(list)
for d in data:
  mx[d['date']].append(d['temp_max'])
  mn[d['date']].append(d['temp_min'])

[{'date': k, 'temp_min': mn[k], 'temp_max': mx[k]} for k in mx]
#[{'date': '2019-05-31', 'temp_min': [51.75, 52.5, 53.29], 
# 'temp_max': [52.25, 52.87, 53.55]}, {'date': '2019-06-01',
# 'temp_min': [68.19, 61.45, 56.77], 'temp_max': 
#  [75.19, 68.45, 59.77]}]

Solution 3:

You might be more successful sticking to a dictionary format:

new_data = {}
for record in data:
  if record['date'] notin new_data.keys():
    new_data[record['date']]={'temp_max':[], 'temp_min' : []}
  # append values
  new_data[record['date']]['temp_max'].append(record['temp_max'])
  new_data[record['date']]['temp_min'].append(record['temp_min'])

Alternatively, you can do this same manipulation in pandas:

df = pd.DataFrame(data)

new_data = []
fordatein df.date.unique():
  df_temp = df[df.date == date]
  temp_max = list(df_temp.temp_max)
  temp_min = list(df_temp.temp_min)

  new_data.append({'date':date, 'temp_max':temp_max, 'temp_min':temp_min})

As a side note, it would be helpful to know what you were using this manipulation for so as best to create something useful for your larger use case.

Solution 4:

Just to show you what I meant in my comment by aiming for a dict of dicts instead of a list of dicts:

from collections import defaultdict
newdict = defaultdict(dict)

for d indata:
    newdict[d['date']]['Tmin'] = newdict[d['date']].get('Tmin', []) + [d['temp_min']]
    newdict[d['date']]['Tmax'] = newdict[d['date']].get('Tmax', []) + [d['temp_max']]

# defaultdict(<class 'dict'>, {'2019-05-31': {'Tmin': [51.75, 52.5, 53.29], 'Tmax': [52.25, 52.87, 53.55]}, '2019-06-01': {'Tmin': [68.19, 61.45, 56.77], 'Tmax': [75.19, 68.45, 59.77]}})   

This would have the advantage that you don't have to search a list at which index which date is stored. You could easily do sth like

newdict['2019-06-01']['Tmin']

and would receive all the Tmin data of the first of June:

[68.19, 61.45, 56.77]

Post a Comment for "How To Create A List Based On Same Value Of A Dictionary Key"