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
  • 112,593
  • 23
  • 157
  • 280
bayman
  • 1,309
  • 3
  • 17
  • 39

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,335
  • 1
  • 10
  • 20
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
  • 9,860
  • 1
  • 20
  • 33