0

I have a list that looks like :

my_list = ['singapore','india','united states','london','paris','india','london','china','singapore','singapore','new york']

Now I want a Counter[dict] of only specific words in this list

Counter(my_list) gives me the following :
Counter({'singapore': 3, 'india': 2, 'london': 2, 'united states': 1, 'paris': 1, 'china': 1, 'new york': 1})

But is there a way to create a Counter of only specific words from a list , like for example Counter of words in ['london','india','singapore']

What would be the fastest way to do this ? My list is very large.

What I tried : my_list.count('london') for example. But is there a faster way to achieve this ?

Ram K
  • 1,746
  • 2
  • 14
  • 23
  • 1
    See if you can find this to be helpful https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item – Harshal Jani Sep 26 '19 at 18:27

1 Answers1

8

You could use a set to filter the words, for example:

from collections import Counter

needles = {'london', 'india', 'singapore'}
haystack = ['singapore', 'india', 'united states', 'london', 'paris', 'india',
            'london', 'china', 'singapore', 'singapore', 'new york']

result = Counter(value for value in haystack if value in needles)
print(result)

Output

Counter({'singapore': 3, 'india': 2, 'london': 2})
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
  • 2
    I like the analogy, what would be awesome is changing `value` to `needle` lol – Jab Sep 26 '19 at 18:19
  • @Daniel thank you , would you know if this is faster than using count without any benchmarks ? – Ram K Sep 26 '19 at 18:19
  • 3
    @RameshK For only 1 word, count is going to be faster, for multiple words this approach is faster. – Dani Mesejo Sep 26 '19 at 18:21
  • 2
    I'm a bit bothered you didn't use `Counter(needle for needle in haystack if needle in needles)`. It upsets me deeply. – r.ook Sep 26 '19 at 18:49