Python Program to find the cube of each list element

PythonServer Side ProgrammingProgramming

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.

raja
Published on 15-Sep-2021 11:30:55
Advertisements