-1

How do you find if str is in a list, more than one time, and the amount?

list = [hi, hi, bye ,hello]
if "hi" in list:
    print("hi is in the list")

That won't work since it does not show the amount that "hi" was in the list.

I would like it to output: "hi" is in the list 2 times. I hope this is a good question.

tian 2.0
  • 1
  • 6

2 Answers2

0

myList=["a","a","a","b","c","c"] myList.count('a') 3

tian 2.0
  • 1
  • 6
0

words = ['hi', 'hi', 'bye', 'hello']

answer = len([x for x in words if x == 'hi'])

You build a new list only using 'hi' and surround it with len(), which will count the number of positions.

Superspork
  • 393
  • 2
  • 9