Reputation: 15
I am currently working on a Guess Who like game for school work and I cannot seem to get this to work. What I am trying to do is get the field "Name" printed from all the dictionaries below within a list.
Greg = {"Name":"Greg", "HairLength":"Short", "HairColour":"Brown", "FacialHair":"Yes", "Jewellery":"Yes", "Hat":"No", "Lipstick":"No", "Gender":"Male"}
Chris = {"Name":"Chris", "HairLength":"Long", "HairColour":"Blonde", "FacialHair":"No", "Jewellery":"No","Hat":"Yes", "Lipstick":"Yes", "Gender":"Male"}
Jason = {"Name":"Jason", "HairLength":"Short", "HairColour":"Brown", "FacialHair":"Yes", "Jewellery":"No","Hat":"Yes", "Lipstick":"No", "Gender":"Male"}
Clancy = {"Name":"Clancy", "HairLength":"Bald", "HairColour":"Red", "FacialHair":"Yes", "Jewellery":"No", "Hat":"No","Lipstick":"No", "Gender":"Male"}
Betty = {"Name":"Betty", "HairLength":"Short", "HairColour":"Blonde", "FacialHair":"No", "Jewellery":"Yes","Hat":"Yes", "Lipstick":"Yes", "Gender":"Female"}
Helen = {"Name":"Helen", "HairLength":"Short", "HairColour":"Brown", "FacialHair":"No", "Jewellery":"No", "Hat":"No","Lipstick":"Yes", "Gender":"Female"}
Selena = {"Name":"Selena", "HairLength":"Long", "HairColour":"Brown", "FacialHair":"No", "Jewellery":"Yes","Hat":"No", "Lipstick":"No", "Gender":"Female"}
Jacqueline = {"Name":"Jacqueline", "HairLength":"Long", "HairColour":"Red", "FacialHair":"Yes", "Jewellery":"Yes", "Hat":"No","Lipstick":"No", "Gender":"Female"}
AISuspects = ([Greg, Chris, Jason, Clancy, Betty, Selena, Helen,
Jacqueline])
UserSuspects = ([Greg, Chris, Jason, Clancy, Betty, Selena, Helen, Jacqueline])
print("AISuspects:")
#Here i want it to print the field "Name" in every dictionary within the list AISuspects
print("UserSuspects:")
#Here i want it to print the field "Name" in every dictionary within the list UserSuspects
Expected output and current output after the solution:
AI Suspects: ['Greg', 'Chris', 'Jason', 'Clancy', 'Betty', 'Selena', 'Helen', 'Jacqueline']
User Suspects: ['Greg', 'Chris', 'Jason', 'Clancy', 'Betty', 'Selena', 'Helen', 'Jacqueline']
Upvotes: 0
Views: 383
Reputation: 1
UserSuspects = [Greg['Name'], Chris['Name'],Jason['Name'], Clancy['Name'], Betty['Name'], Selena['Name'], Helen['Name'], Jacqueline['Name']]
print UserSuspects
Upvotes: -1
Reputation: 11476
You can use a list comprehension to get a list of all the suspects' names
suspects_names = [suspect['Name'] for suspect in AISuspects]
Then you can use print(' '.join(suspect_names))
If you don't mind printing each name in a new line, just use a for loop:
for suspect in AISuspects:
print(suspect['Name'])
Take care of those parenthesis around the lists definitions, you don't need them and they're usually used to define tuples, so I'd get rid of them
Upvotes: 5