Python function program – Write a function lenFOURword(L), where L is the list of elements (list of words) passed as an argument to the function. The function returns another list named ‘indexList’ that stores the indices of all five lettered words of L.

Example:

For example:

If L contains [“PRUTHA”, “DHRUV”, “KRIZA”, “DEV”, “RUSHI”]

The indexList will have [1, 2, 4]

Python function program to store words having 5 letters into separate list from list of words – Explanation:

In this program, a function is declared with a list argument and this list is traversed through for loop. For loop contains if condition to check the length of each word in the list is 5 or not. If the condition evaluates to true, the word will be appended to the indexList and finally, the indexList will be returned at the end of the function.

Python function program to store words having 5 letters into separate list from list of words – Steps

  1. Define a function lenFOURword(L).
  2. Declared an empty list named IndexList inside the function
  3. Travesered the list passed argument in the function header using for loop
  4. Apply the condition to check the words having 5 letters
  5. Append the words into the indexList
  6. Come out of if condition and for loop and return the indexList
  7. Declared a list out of the function
  8. Call the function and pass the list as a parameter

Python function program to store words having 5 letters into separate list from list of words – Code

Here is the code for Python function program to store words having 5 letter into separate list from list of words:

def lenFOURword(L):
  indexList=[]
  for i in range(len(L)):
    if len(L[i])==5:
      indexList.append(i)
  return indexList
    
l=["PIYUSH","SAGAR","MANOJ","RAMESH","PRIYA"]
print(lenFOURword(l))

Python function program to store words having 5 letters into separate list from list of words – Output

Python Function Program - Write a function lenFOURword(L), where L is the list of elements (list of words) passed as an argument to the function. The function returns another list named ‘indexList’ that stores the indices of all five lettered words of L.

Follow this link for more programs:

Python Function Programs

Leave a Reply