w3resource

Python: Multiply each number of given list with a given number using lambda function


21. Multiply List Lambda

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

For more Practice: Solve these Related Problems:

  • Write a Python program to add a given number to each element in a list using lambda.
  • Write a Python program to subtract a given number from each element in a list using lambda.
  • Write a Python program to raise each element in a list to the power of a given number using lambda.
  • Write a Python program to divide each element in a list by a given number, ensuring no division by zero, using lambda.

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.



Follow us on Facebook and Twitter for latest update.