0

I want to print and count the users who are 'male' and are born on 1973, 1980. Is there a way to search '1973' and '1980' in 'birthday' values so I can count them too?

def getMen():
    count = 0
    users = [
        {'name': 'Bronn', 'gender': 'male', 'birthday': '1973-03-23'},
        {'name': 'Reigar', 'gender': 'male', 'birthday': '1973-11-03'},
        {'name': 'Eiegon', 'gender': 'male', 'birthday': '1963-11-03'},
        {'name': 'Sansa', 'gender': 'female', 'birthday': '2012-11-03'},
        {'name': 'Jon', 'gender': 'male', 'birthday': '1980-11-03'},
        {'name': 'Robb', 'gender': 'male', 'birthday': '1980-05-14'},
        {'name': 'Tisha', 'gender': 'female', 'birthday': '2012-11-03'},
        {'name': 'Rick', 'gender': 'male', 'birthday': '2012-11-03'},
        {'name': 'Joffrey', 'gender': 'male', 'birthday': '1999-11-03'},
        {'name': 'Edd', 'gender': 'male', 'birthday': '1973-11-03'}
    ]

    for user in users:
        if user['gender']=='male':
            count +=1
            print(user)
    print(count)
getMen()
0

2 Answers 2

2

You can extract the year as a substring of the birthday and then compare:

    for user in users:
        if user['gender']=='male' and user['birthday'][0:4] in ['1973', '1980']:
            count +=1
            print(user)
Sign up to request clarification or add additional context in comments.

Comments

0

You can filter your list as below and get length of the list to find the count:

count = len(
    [
        user
        for user in users
        if user["gender"] == "male" and user["birthday"].startswith("1973")
    ]
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.