ganiyuisholaafeez

Declaring function using filter()

Feb 29th, 2020
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. """ This code accepts a list of words and a number and it returns only a list of words that has the length of the number specified"""
  2. # For flexibility, user is expected to type words where comma is used as
  3. # as a delimiter between every two word
  4.  
  5. type_words = input("Enter your list of word and use a comma and whitespace\
  6. as a separator between two word: ")
  7. list_of_words = type_words.split(", ") # The splits the words to a list
  8.  
  9. # The length to be considered is typed in by the user
  10. no_of_character = int(input("Enter the choosen length to consider: "))
  11.  
  12. # right_words function declaration
  13. def right_words(words, number):
  14.     print(list(filter(lambda word: len(word) == number, words)))
  15.  
  16. # Invocation of the right_words function
  17. right_words(list_of_words, no_of_character)
  18.  
  19.  
  20. # Enter your list of word and use a comma and whitespaceas a separator between two word:
  21. # cat, dog, bean, ace, heart
  22. # Enter the choosen length to consider: 3
  23. # ['cat', 'dog', 'ace']
  24.  
  25. # Enter your list of word and use a comma and whitespaceas a separator between two word:
  26. # cat, dog, bean, ace, heart
  27. # Enter the choosen length to consider: 5
  28. # ['heart']
  29.  
  30. # Enter your list of word and use a comma and whitespaceas a separator between two word
  31.  
  32. # Enter the choosen length to consider: 4
  33. # []
Add Comment
Please, Sign In to add comment