I have a list:
name_list = ["David Joyner", "David Zuber", "Brenton Joyner",
"Brenton Zuber", "Nicol Barthel", "Shelba Barthel",
"Shelba Crowley", "Shelba Fernald", "Shelba Odle",
"Shelba Fry", "Maren Fry"]
I want to write a function to return a dictionary which has first name as key, all full names associated with the first name as value:
{'Shelba': ['Shelba Barthel', 'Shelba Crowley', 'Shelba Fernald',
'Shelba Odle', 'Shelba Fry'], 'David': ['David Joyner', 'David Zuber'],
'Brenton': ['Brenton Joyner', 'Brenton Zuber'], 'Maren': ['Maren Fry'],
'Nicol': ['Nicol Barthel']}
thanks.
Smart way (1 pass through your list):
from collections import defaultdict
name_list = ["David Joyner", "David Zuber", "Brenton Joyner",
"Brenton Zuber", "Nicol Barthel", "Shelba Barthel",
"Shelba Crowley", "Shelba Fernald", "Shelba Odle",
"Shelba Fry", "Maren Fry"]
rv = defaultdict(list)
for elem in name_list:
rv[elem.split()[0]].append(elem)
print(dict(rv))
Output:
{'David': ['David Joyner', 'David Zuber'],
'Brenton':['Brenton Joyner', 'Brenton Zuber'],
'Nicol': ['Nicol Barthel'],
'Shelba': ['Shelba Barthel','Shelba Crowley','Shelba Fernald','Shelba Odle','Shelba Fry'],
'Maren': ['Maren Fry']}
Slow way (might look nice, but is inefficient):
d = {key:[n for n in name_list if n.startswith(key)]
for key in set( p.split()[0] for p in name_list)}
This is computationally far worse then the defaultdict
version because it traverses the list multiple times:
set( p.split()[0] for p in name_list)
[n for n in name_list if n.startswith(key)]
If you need to sort afterwards, simply use the list.sort()
on your dict
s values:
# sorting the data afterwards
for k in rv:
rv[k].sort(key = lambda x:x.split()[1]) # to sort by last names
Output:
# only difference when sorted: see key `Shelba`
{'David': ['David Joyner', 'David Zuber'],
'Brenton':['Brenton Joyner', 'Brenton Zuber'],
'Nicol': ['Nicol Barthel'],
'Shelba': ['Shelba Barthel','Shelba Crowley','Shelba Fernald','Shelba Fry','Shelba Odle'],
'Maren': ['Maren Fry']}