Reputation: 3
Im trying to keep a separate list of dictionaries from a larger list of dictionaries that have the same dates in format YYYY/MM/DD, for example
d = [{"Date": "2020/10/03 3:30", "Name": "John"}, {"Date": "2020/10/03 5:15", "Name": "Harry"}, {"Date": "2020/10/05 6:30", "Name": "Rob"}]
The output would be
[{"Date": "2020/10/03 3:30", "Name": "John"}, {"Date": "2020/10/03 5:15", "Name": "Harry"}]
Currently I have,
temp = []
for i in range(len(d)):
temp = []
for date in range(len(total)):
if (total[date]['date'][:10] == total[date+1]['date'][:10]) and date+1 != len(total):
temp.append(d[i])
but this doesn't work for most of the cases and not sure how to implement it, any tips ?
Upvotes: 0
Views: 52
Reputation: 11342
You can use list comprehension for this. Just split the date string and compare the date part.
d = [{"Date": "2020/10/03 3:30", "Name": "John"}, {"Date": "2020/10/03 5:15", "Name": "Harry"}, {"Date": "2020/10/05 6:30", "Name": "Rob"}]
dl = set([x['Date'].split()[0] for x in d]) # unique list of dates
dtall = [[x for x in d if x['Date'].split()[0] == dx] for dx in dl] # group by date string
print(dtall)
Output
[[{'Date': '2020/10/05 6:30', 'Name': 'Rob'}],
[{'Date': '2020/10/03 3:30', 'Name': 'John'}, {'Date': '2020/10/03 5:15', 'Name': 'Harry'}]]
--- Loop version ---
d = [{"Date": "2020/10/03 3:30", "Name": "John"}, {"Date": "2020/10/03 5:15", "Name": "Harry"}, {"Date": "2020/10/05 6:30", "Name": "Rob"}]
dl = set() # a set removes duplicates
for dct in d: # each dictionary
dt = dct['Date'].split()[0] # split date\time, keep date
dl.add(dt) # add date to set (duplicates are ignored)
dtall = [] # final list
for dt in dl: # each unique date
dtdate = [] # list for this date
for dct in d: # each dictionary
if dct['Date'].split()[0] == dt: # if date matches
dtdate.append(dct) # add to this date list
dtall.append(dtdate) # add date list to main list
print(dtall)
Output
[[{'Date': '2020/10/05 6:30', 'Name': 'Rob'}],
[{'Date': '2020/10/03 3:30', 'Name': 'John'}, {'Date': '2020/10/03 5:15', 'Name': 'Harry'}]]
Upvotes: 1
Reputation: 14218
you can use collections.defaultdict
. Using a dict the added value you can access sublists by key (i.e. the date). You can always work with just dict.values()
if you prefer.
spam = [{"Date": "2020/10/03 3:30", "Name": "John"},
{"Date": "2020/10/03 5:15", "Name": "Harry"},
{"Date": "2020/10/05 6:30", "Name": "Rob"}]
from collections import defaultdict
from datetime import date
from pprint import pprint
eggs = defaultdict(list)
for item in spam:
d, t = item['Date'].split(' ')
eggs[d].append(item)
pprint(eggs)
pprint(eggs.values())
output
defaultdict(<class 'list'>,
{'2020/10/03': [{'Date': '2020/10/03 3:30', 'Name': 'John'},
{'Date': '2020/10/03 5:15', 'Name': 'Harry'}],
'2020/10/05': [{'Date': '2020/10/05 6:30', 'Name': 'Rob'}]})
dict_values([[{'Date': '2020/10/03 3:30', 'Name': 'John'},
{'Date': '2020/10/03 5:15', 'Name': 'Harry'}],
[{'Date': '2020/10/05 6:30', 'Name': 'Rob'}]])
Upvotes: 0
Reputation: 1
d = [{"Date": "2020/10/03 3:30", "Name": "John"}, {"Date": "2020/10/03 5:15", "Name": "Harry"}, {"Date": "2020/10/05 6:30", "Name": "Rob"}]
tmp = [d[0]]
for i in d[1:]:
flag = False
for j in tmp:
if j['Date'].split(' ')[0] == i['Date'].split(' ')[0]:
flag = True
break
if flag:
tmp.append(i)
maybe this can help you
Upvotes: 0