Filtering values using a for loop

Viewed 36

Now, use a for loop to create a list of all the cat owners who are under the age of 28.

[{'name': 'Daniel',
  'age': 29,
  'job': 'Engineer',
  'pet': 'Cat',
  'pet_name': 'Gato'},
 {'name': 'Katie',
  'age': 30,
  'job': 'Teacher',
  'pet': 'Dog',
  'pet_name': 'Frank'},
 {'name': 'Owen',
  'age': 26,
  'job': 'Sales person',
  'pet': 'Cat',
  'pet_name': 'Cosmo'},
 {'name': 'Josh',
  'age': 22,
  'job': 'Student',
  'pet': 'Cat',
  'pet_name': 'Chat'},
 {'name': 'Estelle',
  'age': 35,
  'job': 'French Diplomat',
  'pet': 'Dog',
  'pet_name': 'Gabby'},
 {'name': 'Gustav',
  'age': 24,
  'job': 'Brewer',
  'pet': 'Dog',
  'pet_name': 'Helen'}]

This is what they gave me:

cat_owners = None
#your code here

This is what I did:

cat_owners = []
for person in people:
    if person["pet"] == "Cat" and person["age"] < 28:
        cat_owners.append(person['name'])
print(cat_owners)

Output: (Exactly what I wanted but I dont know if that is what they asked for...)

['Owen', 'Josh']

My concern is that I dont know why they gave a NoneType! Is there a way to do it that way.

0 Answers
Related