Python: Multiply each number of given list with a given number using lambda function
Python Lambda: Exercise-21 with Solution
Write a Python program that multiplies each number in a list with a given number using lambda functions. Print the results.
Sample Solution:
Python Code :
# Create a list named 'nums' containing integers
nums = [2, 4, 6, 9, 11]
# Assign an integer value of 2 to the variable 'n'
n = 2
# Print the original list 'nums'
print("Original list: ", nums)
# Print the given number stored in variable 'n'
print("Given number: ", n)
# Use the map function to create a new list 'filtered_numbers' by multiplying each number in 'nums' by 'n'
filtered_numbers = list(map(lambda number: number * n, nums))
# Print a label for the upcoming results
print("Result:")
# Convert the elements in the 'filtered_numbers' list to strings and join them with spaces in between
print(' '.join(map(str, filtered_numbers)))
Sample Output:
Original list: [2, 4, 6, 9, 11] Given number: 2 Result: 4 8 12 18 22
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to find the numbers of a given string and store them in a list, display the numbers which are bigger than the length of the list in sorted form. Use lambda function to solve the problem.
Next: Write a Python program that sum the length of the names of a given list of names after removing the names that starts with an lowercase letter. Use lambda function.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/lambda/python-lambda-exercise-21.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics