Python Lists - Finding Number of Times a String Occurs

Viewed 39026

How would I find how many times each string appears in my list?

Say I have the word:

"General Store"

that is in my list like 20 times. How would I find out that it appears 20 times in my list? I need to know this so I can display that number as a type of "poll vote" answer.

E.g:

General Store - voted 20 times
Mall - voted 50 times
Ice Cream Van - voted 2 times

How would I display it in a fashion similar to this?:

General Store
20
Mall
50
Ice Cream Van
2
8 Answers

If you're willing to use the pandas library, this one is really quick:

import pandas as pd 

my_list = lis=["General Store","General Store","General Store","Mall","Mall","Mall","Mall","Mall","Mall","Ice Cream Van"]
pd.Series(my_list).value_counts()
votes=["General Store", "Mall", "General Store","Ice Cream Van","General Store","Ice Cream Van","Ice Cream Van","General Store","Ice Cream Van"]

for vote in set(votes):
    print(vote+" - voted "+str(votes.count(vote))+" times")

Try this. It will output

General Store - voted 4 times
Ice Cream Van - voted 4 times
Mall - voted 1 times
Related