Counting elements in a list

votes = [‘Jake’, ‘Jake’, ‘Laurie’, ‘Laurie’, ‘Laurie’, ‘Jake’, ‘jake’, ‘Jake’, ‘Laurie’, ‘Cassie’, ‘Cassie’, ‘Jake’, ‘Jake’, ‘Cassie’, ‘Laurie’, ‘Cassie’, ‘Jake’, ‘Jake’, ‘Cassie’, ‘Laurie’]

jake_votes=votes.count(‘Jake’ or ‘jake’)


here the expected answer in 9 ,but its comming 8 while using or and 1 while using and. can someone please tell me the logic there any way to modify the code to get the correct answer

All the Jake’s in the code exercise are uppercase. I am assuming you changed it.
Any who…
It only counts the first string, not the following string.
Using another example:
votes.count(“Jake” and “Laurie”) you get the same answer as above because the string ‘Jake’ is first.

Later in the string lesson, you will learn about string methods. In particular, the one used to modify the cases of string. You can take a look forward if you want. However, you will learn it soon. Don’t worry. :slight_smile:

1 Like

Try two variables, one for Jack and one for Jake, then add those for the final answer.

tried using variables but still not working

Here’s the code I did and it worked.

votes = [‘Jake’, ‘Jake’, ‘Laurie’, ‘Laurie’, ‘Laurie’, ‘Jake’, ‘jake’, ‘Jake’, ‘Laurie’, ‘Cassie’, ‘Cassie’, ‘Jake’, ‘Jake’, ‘Cassie’, ‘Laurie’, ‘Cassie’, ‘Jake’, ‘Jake’, ‘Cassie’, ‘Laurie’]

Jake_votes = vote.count('Jake)
jake_votes = vote.count('jake)

total = Jake_votes + jake_votes


Hopefully that helps…


i think this will work but i wanted to know if there was any option to do this in one line.thanks by the way

1 Like

Probably, but I have never done Python 3 so that was a bit of a challenge for me to get the code written out right. I honestly don’t know.

1 Like

If you’re after one line, something like the following would do the job-
total_count = votes.count(‘jake’) + votes.count(‘Jake’)

There may be a number of more generlised ways to do this such as using the str.lower() method but they either can’t or probably shouldn’t be performed on a single line.
There are some disucssions and solutions of more generalised methods at the following links (praise StackOverflow)-

1 Like