Search code examples
jsonlistdictionarypython-3.7

How do I access the value from a dictionary in a list in a dictionary?


for example this is the data:

data = {'number': 3, 'message': 'success', 'people': [{'craft': 'ISS', 'name': 'Chris Cassidy'}, {'craft': 'ISS', 'name': 'Anatoly Ivanishin'}, {'craft': 'ISS', 'name': 'Ivan Vagner'}]}

how do I print only the names of the people in this case?

required output:

('Chris Cassidy', 'Anatoly Ivanishin', 'Ivan Vagner')

I can't wrap my head around this, please help. :)


Solution

  • Try:

    names = []
    for person in data["people"]:
        names.append(person["name"])
    

    names will then have your desired list.