Hello!Please,give a piece of advice.While counting elements in a list,we need to look for a string,we can
t we look for few strings simulteneously(as a list)?Why cant we use votes.count[‘Jake’,‘Laurie’]?
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")
What do you mean by simultaneously? Most things happen in sequence, changing the order that you do it won’t change the result or the amount of work, so what would the difference be?
votes.count([‘Jake’, ‘Laurie’]) returns the number of times the list occurs in a given list. In other words a list in list.
votes = ['Jake', 'Laurie', ['Jake','Laurie'], 'Laurie']
If you want to count the number of times a sequence appears in a list, you could do the following:
count = 0
for i in range(len(votes)):
if votes[i:i+2] == ['Jake','Laurie']:
count += 1
>2 #"Jake', 'Laurie' sequence only appears twice in votes
Another way to do it is with list comprehension:
count = len([i for i in range(len(votes)) if votes[i:i+2] == ['Jake','Laurie']])
Hello, @4774619062, and welcome to the Codecademy Forums!
You may be able to write a function that accomplishes the task that you are trying to perform. But first you need to define unambiguously what that task is. Are you trying to count the total number of votes for all of the names that you provide? Are you instead attempting to obtain a separate count for each of the names? We need to know precisely what information you would like the program to provide.