Advertisement
KaySawbridge

Count strings in a list

Jul 27th, 2020 (edited)
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.43 KB | None | 0 0
  1. #Count number of occurrences of strings in a list
  2.  
  3. def count_string(list_element,item_list):
  4.     count = 0
  5.     for item in item_list:
  6.         if list_element == item:
  7.             count += 1
  8.     return count
  9.    
  10. fruit = ["apples", "bananas", "pears", "apples", "oranges", "pears", "apples", "pears", "lime", "oranges"]
  11.  
  12.  
  13. fruit_number = count_string("pears",fruit)
  14.  
  15.  
  16. print("There are {0} in the list".format(fruit_number))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement