0

I writing a function on determining how many females and males there are in a large list (200,000+). How do I only count the females in that list?

Right now what I have is a function that just counts the number of words in a list:

genders = [Male, Female, Female, Female, Male, Male, Female, Female]  #This is also saying "name 'Female' is not defined"

def gender(genders):  
        Female = len(genders)  
        return Female  # --> returns 4      
  • Strings need quotes around them to be strings. Otherwise they're treated as variable names. – ggorlen Apr 16 '21 at 01:19
  • Please go through the [intro tour](https://stackoverflow.com/tour), the [help center](https://stackoverflow.com/help) and [how to ask a good question](https://stackoverflow.com/help/how-to-ask) to see how this site works and to help you improve your current and future questions, which can help you get better answers. – Prune Apr 16 '21 at 01:32
  • See [How much research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) and the [Question Checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). It appears that you made no attempt to look up how to do this before posting. – Prune Apr 16 '21 at 01:32

1 Answers1

1

list.count() is the most Pythonic way to handle this, simple easy to understand.

genders = ['Male', 'Female', 'Female', 'Female', 'Male', 'Male', 'Female', 'Female']
genders.count('Female')
#5

In a function

def count_gender(gender, data):
    return data.count(gender)

count_gender('Male', genders)
#3

count_gender('Female', genders)
#5

In a loop

females = 0

for gender in genders:
    if gender == 'Female':
        females += 1

females
#5
PacketLoss
  • 5,561
  • 1
  • 9
  • 27