Syntax error Python Program to find the cube of each list element

Python Program to find the cube of each list element



When it is required to find the cube of each list element, a simple iteration and the ‘append’ method are used.

Example

Below is a demonstration of the same

my_list = [45, 31, 22, 48, 59, 99, 0]

print("The list is :")
print(my_list)
my_result = []
for i in my_list:
   my_result.append(i*i*i)

print("The resultant list is :")
print(my_result)

Output

The list is :
[45, 31, 22, 48, 59, 99, 0]
The resultant list is :
[91125, 29791, 10648, 110592, 205379, 970299, 0]

Explanation

  • A list is defined and is displayed on the console.

  • An empty list is defined.

  • The original list is iterated over.

  • Every element is multiplied with itself thrice.

  • This result is appended to the empty list.

  • This is the result that is displayed on the console.

Updated on: 2021-09-15T11:30:55+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements