0

I have a list:

my_list = ['coffee', 'sunshine', 'hiking', 'stocks', 'mountains', 'space', 'Travel']

I would like to count the occurrence of a specific letter across all the elements in that list, let's say the letter 's'.

Can this be achieved without loops?

1
  • 1
    collections.Counter("".join(my_list))["s"] Commented Dec 23, 2020 at 21:17

2 Answers 2

1

Join the words, you then get a string where you can use the count() method to get the number of occurence of a letter.

"".join(my_list).count("a")
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the map function provided you want the count of the character in each string in the given list.

my_list = ['coffee', 'sunshine', 'hiking', 'stocks', 'mountains', 'space', 'Travel']

count_s = list(map(lambda n: n.count('s'), my_list))
print(count_s)

[0, 2, 0, 2, 1, 1, 0]

1 Comment

Better to avoid map(lambda)... - instead you could do this [w.count('s') for w in lst]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.