Computer Science, asked by rajkumarsahani0012, 1 year ago

Using lambda and map functions, write a program which takes a list of words as input and prints a list of lengths of each of the words.

Input: ["hello", "my", "name", "is", "jupyter", "notebook"]
Output: [5, 2, 4, 2, 7, 8]

Answers

Answered by fiercespartan
2

The lambda function is an extremely important function in python, it makes our code easier and shorter.

Lambda is like def() but just one word and it is also one time use. We can not use the lambda function again an again but the def() function can be called and used any time in code.

Comparison of the def and lambda functions:

def code(x):                      

   return 5*x      

But for lambda:

code = lambda x:x*5    

__________________________________________

But in the case of this question, we will have to check everything in the list and then print the length.

For that, I will be using map() function. This function applies the function we write to each and every single character in either list,set or tuple.

Here is how you write a code for the problem:

CODE:

words =  ["hello", "my", "name", "is", "jupyter", "notebook"]

print( list(map(lambda x:len(x) , words))))

__________________________________________

If you want to take input from the user, here is how:

words = input('Enter the words separated by a comma:').split(',')

print( list(map(lambda x:len(x) , words))))

__________________________________________

Similar questions