0

How do I find and print the name and age of 'id' == 52? Is using a for loop the only way?

name_list = [
    {'id': 11, 'name': 'John', 'Age': 22},
    {'id': 52, 'name': 'Mary', 'Age': 25},
    {'id': 9, 'name': 'Carl', 'Age': 55 }
]
martineau
  • 119,623
  • 25
  • 170
  • 301
bayman
  • 1,579
  • 5
  • 23
  • 46

2 Answers2

1

What you are looking for is a standard loop through a list.

for i in name_list:
    if i['id'] == 52:
        print(i['name'])
        print(i['Age'])
Rashid 'Lee' Ibrahim
  • 1,357
  • 1
  • 9
  • 21
0

The quickest way I can think of is to use a loop in list comprehension form:

In [1]: [x for x in name_list if x['id'] == 52]
Out[1]: [{'id': 52, 'name': 'Mary', 'Age': 25}]
Peter Leimbigler
  • 10,775
  • 1
  • 23
  • 37