0

I am new to coding and I am trying to extract 2 lists from the following nested dictionary:

dict : {'player2': {'player': 'player2', 'firstname': 'Ross', 'lastname': 'Estrada'}, 'player3': {'player': 'player3', 'firstname': 'Melvin', 'lastname': 'Graves'}, 'player0': {'player': 'player0', 'firstname': 'Alfredo', 'lastname': 'Hopkins'}, 'player1': {'player': 'player1', 'firstname': 'Jay', 'lastname': 'Ramos'}, 'player6': {'player': 'player6', 'firstname': 'Claudia', 'lastname': 'Brown'}, 'player7': {'player': 'player7', 'firstname': 'Ben', 'lastname': 'Carson'}, 'player4': {'player': 'player4', 'firstname': 'Virginia', 'lastname': 'Harmon'}, 'player5': {'player': 'player5', 'firstname': 'Caleb', 'lastname': 'Reese'}, 'player8': {'player': 'player8', 'firstname': 'Olivia', 'lastname': 'Olson'}, 'player9': {'player': 'player9', 'firstname': 'Amy', 'lastname': 'Valdez'}}

The first list should contain all the first names, whereas the second list should contain all the second names.

The problem is that I do not know what key to use to signal all outer keys rather than just one.

So, for example I know dict["player2"]["firstname"] will return the player2's firstname, but how can I write this to extract all the firstnames at once.

Thanks for your help!

Florence
  • 59
  • 5

5 Answers5

3
>>> list(zip(*map(operator.itemgetter('firstname', 'lastname'), D.values())))
[('Ross', 'Melvin', 'Alfredo', 'Jay', 'Claudia', 'Ben', 'Virginia', 'Caleb', 'Olivia', 'Amy'), ('Estrada', 'Graves', 'Hopkins', 'Ramos', 'Brown', 'Carson', 'Harmon', 'Reese', 'Olson', 'Valdez')]
>>> fnames, lnames = zip(*map(operator.itemgetter('firstname', 'lastname'), D.values()))
>>> fnames
('Ross', 'Melvin', 'Alfredo', 'Jay', 'Claudia', 'Ben', 'Virginia', 'Caleb', 'Olivia', 'Amy')
>>> lnames
('Estrada', 'Graves', 'Hopkins', 'Ramos', 'Brown', 'Carson', 'Harmon', 'Reese', 'Olson', 'Valdez')
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

You can use a list comprehension and iterate over all outer keys:

d = {
    'player2': {'player': 'player2', 'firstname': 'Ross', 'lastname': 'Estrada'}, 
    'player3': {'player': 'player3', 'firstname': 'Melvin', 'lastname': 'Graves'}, 
    'player0': {'player': 'player0', 'firstname': 'Alfredo', 'lastname': 'Hopkins'}, 
    'player1': {'player': 'player1', 'firstname': 'Jay', 'lastname': 'Ramos'}, 
    'player6': {'player': 'player6', 'firstname': 'Claudia', 'lastname': 'Brown'}, 
    'player7': {'player': 'player7', 'firstname': 'Ben', 'lastname': 'Carson'}, 
    'player4': {'player': 'player4', 'firstname': 'Virginia', 'lastname': 'Harmon'}, 
    'player5': {'player': 'player5', 'firstname': 'Caleb', 'lastname': 'Reese'}, 
    'player8': {'player': 'player8', 'firstname': 'Olivia', 'lastname': 'Olson'}, 
    'player9': {'player': 'player9', 'firstname': 'Amy', 'lastname': 'Valdez'}} 

firstNames = [ d[pKey].get('firstname','') for pKey in d] 
lastNames  = [ d[pKey].get('lastname','')  for pKey in d]

print(firstNames)
print(lastNames)

By using dict.get(key,default) you can avoid getting key-errors on the inner dictionaries in case one has the key you look for not inside it - and provide a default.

See f.e. Why dict.get(key) instead of dict[key]? for answers on why its smart.

Using for k in dictName will iterate overall possible keys of this dictionary, the rest is a simple list comprehension to generate the wanted lists.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
1

That one-liner will do

first_names, last_names = zip(*((d[k]['firstname'], d[k]['lastname']) for k in d))

Output:

>>> first_names
('Virginia', 'Amy', 'Melvin', 'Claudia', 'Jay', 'Alfredo', 'Ross', 
'Caleb', 'Ben', 'Olivia')
>>> last_names
('Harmon', 'Valdez', 'Graves', 'Brown', 'Ramos', 'Hopkins', 'Estrada', 
'Reese', 'Carson', 'Olson')

Note 1: Maybe you would prefer dict.get(key) to dict[key], as suggested by Patrick Artner Note 2: Those variables will be tuples. Convert them to list if you want to.

Scarabyte
  • 303
  • 1
  • 5
0

Another way of doing it is this (naming your dictionary d instead of dict, as dict is a Python built-in type and reserved keyword):

firstNames = list(map(lambda i: d[i]['firstname'], d.keys()))
lastNames = list(map(lambda i: d[i]['lastname'], d.keys()))

print(firstNames)
print(lastNames)

Output:

['Ross', 'Melvin', 'Alfredo', 'Jay', 'Claudia', 'Ben', 'Virginia', 'Caleb', 'Olivia', 'Amy']
['Estrada', 'Graves', 'Hopkins', 'Ramos', 'Brown', 'Carson', 'Harmon', 'Reese', 'Olson', 'Valdez']
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
0

You can use zip and unpacking:

d = {'player2': {'player': 'player2', 'firstname': 'Ross', 'lastname': 'Estrada'}, 'player3': {'player': 'player3', 'firstname': 'Melvin', 'lastname': 'Graves'}, 'player0': {'player': 'player0', 'firstname': 'Alfredo', 'lastname': 'Hopkins'}, 'player1': {'player': 'player1', 'firstname': 'Jay', 'lastname': 'Ramos'}, 'player6': {'player': 'player6', 'firstname': 'Claudia', 'lastname': 'Brown'}, 'player7': {'player': 'player7', 'firstname': 'Ben', 'lastname': 'Carson'}, 'player4': {'player': 'player4', 'firstname': 'Virginia', 'lastname': 'Harmon'}, 'player5': {'player': 'player5', 'firstname': 'Caleb', 'lastname': 'Reese'}, 'player8': {'player': 'player8', 'firstname': 'Olivia', 'lastname': 'Olson'}, 'player9': {'player': 'player9', 'firstname': 'Amy', 'lastname': 'Valdez'}}

first, last = zip(*[[b['firstname'], b['lastname']] for _, b in d.items()])

Output:

('Ross', 'Melvin', 'Alfredo', 'Jay', 'Claudia', 'Ben', 'Virginia', 'Caleb', 'Olivia', 'Amy')

('Estrada', 'Graves', 'Hopkins', 'Ramos', 'Brown', 'Carson', 'Harmon', 'Reese', 'Olson', 'Valdez')
Ajax1234
  • 69,937
  • 8
  • 61
  • 102