FAQ: Working with Lists in Python - Counting elements in a list

This community-built FAQ covers the “Counting elements in a list” exercise from the lesson “Working with Lists in Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Counting elements in a list

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

3 posts were split to a new topic: I made a function to tell me who wins

2 posts were split to a new topic: Is there a way to count letters in a list of words?

I noticed that when I typed ‘jake’ all lowercase, the output was an error. How do I make the counts for Jake count but ignoring upper and lower case.

We would want to coerce the inputs to lowercase before any conditional, or at least on the state we supply…

def starts_with_a(s):
    return s.lower() === 'a'

I’m trying to add a function to automatically display the winner - when i execute winner i get a 0x error -
what am i doing wrong? thanks

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’)
print(jake_votes)

laurie_votes = votes.count(‘Laurie’)
print(laurie_votes)

def winner(jake_votes, laurie_votes):
if jake_votes < laurie_votes:
return ‘Laurie is the winner’

else:
return ‘Jake wins’

Assuming the indentation is correct, your function should work as intended. You can preserve formatting and indentation by using the </> button when pasting your code. Feel free to repost your formatted code so that the indentation can be confirmed to be correct.

Thanks for the indent tip -
You peeps are so helpful!

I have so many questions - i have to limit how much i post - I would probably get banned if I posted every question that came into my head

votes = ['Jake', 'Jake', 'Laurie', 'Laurie', 'Laurie', 'Jake', 'Jake', 'Jake', 'Laurie', 'Cassie', 'Cassie', 'Jake', 'Jake', 'Cassie', 'Laurie', 'Cassie', 'Jake', 'Jake', 'Cassie', 'Laurie', 'Laurie,' 'Laurie,'  'Laurie,' 'Laurie,' 'Laurie,' 'Laurie,' 'Laurie,' 'Laurie,' 'Laurie,' 'Laurie,' 'Laurie,' 
jake_votes = votes.count('Jake')
print(jake_votes)

laurie_votes = votes.count('Laurie')
print(laurie_votes)

cassie_votes = votes.count('Cassie')
print(cassie_votes)

def winner(jake_votes, laurie_votes, Cassie_votes):
  if jake_votes > cassie_votes and jake_votes > laurie_votes:
    return 'Jake wins'

  elif cassie_votes > jack_votes and cassie_votes > laurie_votes:
    return 'Cassie Wins'

  else:
    laurie_votes > jack_votes and laurie_votes > cassie_votes
    return 'Laurie Wins'



print(winner(jake_votes, laurie_votes, cassie_votes))

Ok - Ive managed to sort my original problem - I needed to call print(list(a,b,c)

But now - my math is flawed - i seemed to have inadvertently rigged the election and can’t see why - Jake always wins!
Help !

Hi @ewanhackett464734658,

The first line in your posted code has no closing square bracket at the end, so its syntax is invalid. It also contains multiple occurrences of 'Laurie,', each separated by a space. Note that the comma is included within the quotes.

In addition to what @appylpye mentioned, which is the main cause of the issue, you’ve capitalized the last parameter for winner. Remember that Python is case sensitive. The way you’ve written your code means that it doesn’t have any effect on the final result, but one thing you should note is that it’s often helpful to have names for your function parameters that aren’t already names of variables.

1 Like

Another thing to be alert for is the possibility of a tie vote. Your particular data might not lead to such an outcome, but another set of data could.

1 Like

Is the .count() algorithm a function or method? It returns a value but can’t (as far as I can tell) be called on its own (e.g. count()).

Welcome to the forums!

.count() is an instance method. This means that it can only be called on an object, in this case, of the list data type. Instance methods can only be called on objects (instances of a class, e.g. .count(), note the period at the beginning of the method call) and class methods are called on their own (e.g. count()).

When you think about it in the case of .count(), this makes a lot of sense. We want to count the number of occurences of a specified element in a list. By calling the method on an object, in this case a list, it’s clear that we want to find the occurences of an element in the specific list on which we called the method.

i was trying to make this function for the “election” and make it so that it could handle changes in the main list. the If/else is a little mess, and suggestions to get the same result in cleaner or less code?

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

mail_in_votes = [‘Laurie’, ‘Laurie’, ‘Laurie’, ‘Laurie’, ‘Laurie’, ‘Laurie’]

votes = votes + mail_in_votes

candidates = [‘Jake’, ‘Laurie’, ‘Cassie’,]

vote_totals = [votes.count(‘Jake’), votes.count(‘Laurie’), votes.count(‘Cassie’)]

election_results = list(zip(candidates, vote_totals))

print(election_results)

def announce_winner (results):

if results[0][1] > results[1][1] and results[0][1] > results[2][1]:

print('Jake Won')

elif results[1][1] > results[0][1] and results[1][1] > results[2][1]:

winner = results[1][0]

print(winner)

else:

winner = results[2][0]

print(winner)

announce_winner(election_results)

Using the count method in what ways can we count the amount of things appearing a 2D list , as if the contents of the lists inside were separate?
For example i want to count the amount of 14s in this 2D list :
example = [[12, 14], [14,15], [40, 14, 14]]
or maybe to count the amount found in only one of the lists inside.

One way would be to flatten the list and then count, or perhaps the simpler approach will be to iterate the elements and count, then add to an accumulator

a = [[12, 14], [14,15], [40, 14, 14]]
c = 0
for x in a:
    c += x.count(14)

print (c)    #  4

In this example given in this exercise, how can I count how many “100” are in total?
I’ve tried the following and didn’t succeed:

number_collection = [[100, 200], [100, 200], [475, 29], [34, 34]] num_pairs = number_collection.count(100) print(num_pairs)

list.count() only works on a single dimension list, as far as I know. Similar to the in membership test, it cannot see inside embedded list objects:

>>> number_collection = [[100, 200], [100, 200], [475, 29], [34, 34]]
>>> 100 in number_collection
False
>>>

Consider this basic algo:

>>> count = 0
>>> for x in number_collection:
...     count += 1 if x[0] == 100 else 0
... 
...     
>>> count
2
>>> 

That is based upon us knowing that 100 is at the first index. If we want to test both indices,

>>> count = 0
>>> for x in number_collection:
...     count += 1 if x[0] == 34 else 0
...     count += 1 if x[1] == 34 else 0
... 
...     
>>> count
2
>>> 

Please post a link to the exercise so we can study the narrative and instructions, and see my own code implementation.

Thanks already, @mtf !

Here’s the link to the exercise I was referring to: Counting in a list

I’m sorry I didn’t write it before :sweat_smile:

1 Like