-1

I was just thinking, how could i make a while loop to cycle through a list and count instances of the word 'hello' for example.

list = ['bob','hello','jacob','hello','count']

so expected output would be 2.

john
  • 11
  • 3
    what did you try? python has a REPL that you can use to test out ideas really quickly. that also helps you in learning how to help yourself using help() and dir() and other tools. by investigating for yourself, you'll learn more about the language. – matias elgart Nov 13 '16 at 12:53

2 Answers2

2

Just use count (bonus: you even have "count" as an item in your list ^^):

>>> ['bob','hello','jacob','hello','count'].count('hello')
2
Idos
  • 15,053
  • 14
  • 60
  • 75
0

However if you want to search without case, you can try

word = 'hello'
list = ['bob','hello','jacob','hello','count']
count = len([i for i in list if i.lower() == word.lower()])