Python Forum
Function to count words in a list up to and including Sam
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function to count words in a list up to and including Sam
#6
lst = ["able","been","state","Sam","beer"]
def count_to_sam(samlist): #better name for function
    return_count = 0 #this will be the count of words
    for element in samlist: #loop is in the function
        return_count = return_count+1 #or use return_count += 1
        if element == "Sam":
            break
    return return_count #function returns the count
print(count_to_sam(lst))
Reply


Messages In This Thread
RE: Function to count words in a list up to and including Sam - by Serafim - Feb-16-2021, 03:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,376 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,671 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  How to get unique entries in a list and the count of occurrence james2009 5 3,026 May-08-2022, 04:34 AM
Last Post: ndc85430
  Including data files in a package ChrisOfBristol 4 2,604 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  Not including a constructor __init__ in the class definition... bytecrunch 3 12,074 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  count item in list korenron 8 3,539 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,871 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  list.count does not appear to function Oldman45 7 3,997 Mar-16-2021, 04:25 PM
Last Post: Oldman45
  how to create pythonic codes including for loop and if statement? aupres 1 1,948 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  How to use the count function from an Excel file using Python? jpy 2 4,493 Dec-21-2020, 12:30 AM
Last Post: jpy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020