Dec-25-2021, 09:05 AM
I think that there are several things that could be improved.
Naming: there is no key, value in tuple and therefore such naming is misleading (this tuple actually has string and dictionary in it). If you want to unpack the tuple then give meaningful names, in this case it could be
No need to build separate/temporary list: Counter plays nicely with other parts of Python so one can feed generator or list comprehension directly to it.
So alternatively it can be written as:
Naming: there is no key, value in tuple and therefore such naming is misleading (this tuple actually has string and dictionary in it). If you want to unpack the tuple then give meaningful names, in this case it could be
for email, record in data:
No need to build separate/temporary list: Counter plays nicely with other parts of Python so one can feed generator or list comprehension directly to it.
So alternatively it can be written as:
from collections import Counter data = [('[email protected]',{'first_name':'john','last_name':'doe'}), ('[email protected]',{'first_name':'jane','last_name':'doe'}), ('[email protected]',{'first_name':'derek','last_name':'zoolander'}), ('[email protected]',{'first_name':'murph','last_name':'cooper'}), ('[email protected]',{'first_name':'ned','last_name':'stark'})] last_names = Counter(row[1]['last_name'] for row in data) print(*(f'{k}: {v}' for k, v in last_names.items()), sep='\n')
Output:doe: 2
zoolander: 1
cooper: 1
stark: 1
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.