In this Python list program, we will calculate the factorial of each item in the list with the help of the below-given steps.
Factorial of each item in list:
Steps to solve the program
- Take a list as input.
- Create a function for calculate factorial of a number.
- Create a new list and calculate the factorial of each number from the given list using list comprehension.
- Print the new list to see the output.
#Creating function
def factorial(n):
fact = 1
for i in range(1, n + 1):
fact *= i
return fact
#Input list
list1 = [1, 2, 3, 4]
list1_factorials = [factorial(value) for value in list1]
#Printing output
list1_factorials
Output :
[1, 2, 6, 24]
Related Articles
Python program to get a list of Fibonacci numbers from 1 to 20.
Python program to reverse all the numbers in a given list.
Python program to get palindrome numbers from a given list.
Python program to get a count of vowels in the given list.
Python program to get the list of prime numbers in a given list.
Python program to get a list with n elements removed from the left and right.