Conditional Count in Python
Question:
If I have the following:
team = [
{'name': 'Ben', 'age': 31, 'country': 'France', 'hobbies': ['coding', 'biking']},
{'name': 'Quinn', 'age': 26, 'country': 'Ireland', 'hobbies': ['skiing']},
{'name': 'Sasha', 'age': 24, 'country': 'Lebanon', 'hobbies': ['sports']},
{'name': 'Alex', 'age': 28, 'country': 'Austria', 'hobbies': []}
]
How can I cound the items in ‘hobbies’ for an person, let’s say Ben. I tried let(), sum(), and some If statements, but none work. Maybe I’m just wrong with the syntax or missing a step. Any help to point me in the right direction?
How would I be able to print the number of hobbies listed for example, for Ben 2, for Sasha 1, etc.
Answers:
Use len
, like so:
team = [
{'name': 'Ben', 'age': 31, 'country': 'France', 'hobbies': ['coding', 'biking']},
{'name': 'Quinn', 'age': 26, 'country': 'Ireland', 'hobbies': ['skiing']},
{'name': 'Sasha', 'age': 24, 'country': 'Lebanon', 'hobbies': ['sports']},
{'name': 'Alex', 'age': 28, 'country': 'Austria', 'hobbies': []}
]
for player in team:
print('%s has %d hobbies' % (player['name'], len(player['hobbies'])))
Output:
Ben has 2 hobbies
Quinn has 1 hobbies
Sasha has 1 hobbies
Alex has 0 hobbies
Well, you could try something like:
counts = {person["name"]:len(person.get("hobbies",[])) for person in team}
Or you can use .__len__()
for solution:
team = [
{'name': 'Ben', 'age': 31, 'country': 'France', 'hobbies': ['coding', 'biking']},
{'name': 'Quinn', 'age': 26, 'country': 'Ireland', 'hobbies': ['skiing']},
{'name': 'Sasha', 'age': 24, 'country': 'Lebanon', 'hobbies': ['sports']},
{'name': 'Alex', 'age': 28, 'country': 'Austria', 'hobbies': []}
]
for person in team:
if person["hobbies"].__len__() > 1:
print(person["name"],"has",str(person["hobbies"].__len__()),"hobbies")
else:
print(person["name"],"has",str(person["hobbies"].__len__()),"hobby")
output:
Ben has 2 hobbies
Quinn has 1 hobby
Sasha has 1 hobby
Alex has 0 hobby
If I have the following:
team = [
{'name': 'Ben', 'age': 31, 'country': 'France', 'hobbies': ['coding', 'biking']},
{'name': 'Quinn', 'age': 26, 'country': 'Ireland', 'hobbies': ['skiing']},
{'name': 'Sasha', 'age': 24, 'country': 'Lebanon', 'hobbies': ['sports']},
{'name': 'Alex', 'age': 28, 'country': 'Austria', 'hobbies': []}
]
How can I cound the items in ‘hobbies’ for an person, let’s say Ben. I tried let(), sum(), and some If statements, but none work. Maybe I’m just wrong with the syntax or missing a step. Any help to point me in the right direction?
How would I be able to print the number of hobbies listed for example, for Ben 2, for Sasha 1, etc.
Use len
, like so:
team = [
{'name': 'Ben', 'age': 31, 'country': 'France', 'hobbies': ['coding', 'biking']},
{'name': 'Quinn', 'age': 26, 'country': 'Ireland', 'hobbies': ['skiing']},
{'name': 'Sasha', 'age': 24, 'country': 'Lebanon', 'hobbies': ['sports']},
{'name': 'Alex', 'age': 28, 'country': 'Austria', 'hobbies': []}
]
for player in team:
print('%s has %d hobbies' % (player['name'], len(player['hobbies'])))
Output:
Ben has 2 hobbies
Quinn has 1 hobbies
Sasha has 1 hobbies
Alex has 0 hobbies
Well, you could try something like:
counts = {person["name"]:len(person.get("hobbies",[])) for person in team}
Or you can use .__len__()
for solution:
team = [
{'name': 'Ben', 'age': 31, 'country': 'France', 'hobbies': ['coding', 'biking']},
{'name': 'Quinn', 'age': 26, 'country': 'Ireland', 'hobbies': ['skiing']},
{'name': 'Sasha', 'age': 24, 'country': 'Lebanon', 'hobbies': ['sports']},
{'name': 'Alex', 'age': 28, 'country': 'Austria', 'hobbies': []}
]
for person in team:
if person["hobbies"].__len__() > 1:
print(person["name"],"has",str(person["hobbies"].__len__()),"hobbies")
else:
print(person["name"],"has",str(person["hobbies"].__len__()),"hobby")
output:
Ben has 2 hobbies
Quinn has 1 hobby
Sasha has 1 hobby
Alex has 0 hobby