Python: Multiply all the numbers in a given list using lambda
Python Lambda: Exercise-43 with Solution
Write a Python program to multiply all the numbers in a given list using lambda.
Python Code :
from functools import reduce def mutiple_list(nums): result = reduce(lambda x, y: x*y, nums) return result nums = [4, 3, 2, 2, -1, 18] print ("Original list: ") print(nums) print("Mmultiply all the numbers of the said list:",mutiple_list(nums)) nums = [2, 4, 8, 8, 3, 2, 9] print ("\nOriginal list: ") print(nums) print("Mmultiply all the numbers of the said list:",mutiple_list(nums))
Original list: [4, 3, 2, 2, -1, 18] Mmultiply all the numbers of the said list: -864 Original list: [2, 4, 8, 8, 3, 2, 9] Mmultiply all the numbers of the said list: 27648
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to calculate the product of a given list of numbers using lambda.
Next: Write a Python program to calculate the average value of the numbers in a given tuple of tuples using lambda.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Splits values into two groups. If an element in filter is True, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group
def tips_bifurcate(lst, filter): return [ [x for i, x in enumerate(lst) if filter[i] == True], [x for i, x in enumerate(lst) if filter[i] == False] ] print(tips_bifurcate(['little', 'boo', 'peep', 'sat'], [True, True, False, True]))
[['little', 'boo', 'sat'], ['peep']]
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework