Python Program to find the cube of each list element
Last Updated :
28 Apr, 2023
Given a list, the task is to write a python program to cube all the list elements.
Input: [1, 2, 3, 4]
Output: [1, 8, 27, 64]
Explanation: Cubing all the list elements
Input: [2, 4, 6]
Output: [8, 64, 216]
Method 1: Using loop
This is the brute force way. In this, we just multiply the same element two times by itself.
Example:
Python3
# Initializing list
l = [1, 2, 3, 4]
# Cube List using loop
res = []
for i in l:
res.append(i*i*i)
# printing result
print(res)
Output:
[1, 8, 27, 64]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2: Using Recursive method
Algorithm:
- Define a function "cube_list_recursive(lst)" that takes a list "lst" as input.
- Check if the length of the input list is 0 or not. If the length is 0, return an empty list.
- If the length is not 0, then cube the first element of the list and add it to the result of the recursive call of the function with the remaining list elements.
- Return the result list.
Python3
def cube_list_recursive(lst):
if len(lst) == 0:
return []
else:
return [lst[0]**3] + cube_list_recursive(lst[1:])
# Initializing list
l = [1, 2, 3, 4]
# Cube List using loop
res = cube_list_recursive(l)
# printing result
print(res)
Time complexity: O(N)
Where "n" is the length of the input list since it has to iterate over all the elements of the list once to cube each element.
Auxiliary Space: O(N)
Since it uses recursion and creates a new list with the cube of each element in the input list.
Method 3: Using pow() function
This is also the brute force way. In this, we use in-built pow() function
Example:
Python3
# Initializing list
l = [1, 2, 3, 4]
# Cube List using loop
res = []
for i in l:
res.append(pow(i, 3))
# printing result
print(res)
Output:
[1, 8, 27, 64]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using list comprehension
This task can also be performed using list comprehension. This is similar to above function. Just the difference is that its compact and one liner.
Example:
Python3
# Initializing list
l = [1, 2, 3, 4]
# Cube List using list comprehension
res = [pow(i, 3) for i in l]
# printing result
print(res)
Time complexity: O(n)
Where n is the length of the test_list. The list comprehension takes O(n) time to create the final list.
Auxiliary Space: O(1)
Extra space required is not required
Method 5: Using lambda
This can also be achieved using the lambda function
Example:
Python3
# Initializing list
l = [1, 2, 3, 4]
res = list(map(lambda x: x ** 3, l))
print(res)
Output:
[1, 8, 27, 64]
Method 6 : Using operator.pow() method
Approach
- Initiated for loop to traverse the list elements
- Raised each element to power 3 using operator.pow() and appended to output list
- Displayed output list
Python3
# Initializing list
l = [1, 2, 3, 4]
# Cube List using loop
res = []
import operator
for i in l:
res.append(operator.pow(i, 3))
# printing result
print(res)
Time Complexity : O(N)
Auxiliary Space : O(N)
Method 7: using the map() function
Use a lambda function to cube each item in the list l. The map() function applies this lambda function to each item of l and returns a new iterable with the results. Finally, the list() function is used to convert the iterable to a list.
Python3
# Initializing list
l = [1, 2, 3, 4]
# Cube List using map()
res = list(map(lambda x: x**3, l))
# printing result
print(res)
Time Complexity: O(N), where N is the length of the input list,
Auxiliary Space : O(N)
Using numpy:
Here's an approach using NumPy:
Algorithm:
Convert the given list into a NumPy array.
Use np.power() function to cube each element of the array.
Convert the NumPy array back to a list.
Print the resulting list.
Python3
import numpy as np
# Initializing list
l = [1, 2, 3, 4]
# Convert list to numpy array
arr = np.array(l)
# Cube array elements using np.power() function
cubed_arr = np.power(arr, 3)
# Convert NumPy array back to list
res = cubed_arr.tolist()
# Printing result
print(res)
Output:
[1, 8, 27, 64]
Time Complexity: O(n), where n is the length of the input list. The np.power() function iterates over all elements of the NumPy array once to cube each element.
Space Complexity: O(n), since the NumPy array and resulting list both require O(n) space.
Similar Reads
Python - Operation to each element in list Given a list, there are often when performing a specific operation on each element is necessary. While using loops is a straightforward approach, Python provides several concise and efficient methods to achieve this. In this article, we will explore different operations for each element in the list.
2 min read
Python - Operation to each element in list Given a list, there are often when performing a specific operation on each element is necessary. While using loops is a straightforward approach, Python provides several concise and efficient methods to achieve this. In this article, we will explore different operations for each element in the list.
2 min read
Python Program to find the Next Nearest element in a Matrix Given a matrix, a set of coordinates and an element, the task is to write a python program that can get the coordinates of the elements next occurrence. Input : test_list = [[4, 3, 1, 2, 3], [7, 5, 3, 6, 3], [8, 5, 3, 5, 3], [1, 2, 3, 4, 6]], i, j = 1, 3, K = 3 Output : (1, 4) Explanation : After (1
4 min read
Python Program to Find Cube of a Number We are given a number and our task is to find the cube of this number in Python. The cube of a number is calculated by multiplying the number by itself twice. For example, if the input is 3, then the output will be 3 * 3 * 3 = 27. In this article, we will learn different ways to find the cube of a n
2 min read
How to Get the Number of Elements in a Python List In Python, lists are one of the most commonly used data structures to store an ordered collection of items.In this article, we'll learn how to find the number of elements in a given list with different methods.ExampleInput: [1, 2, 3.5, geeks, for, geeks, -11]Output: 7Let's explore various ways to do
3 min read
Python - Check if element is present in tuple We are given a tuple and our task is to find whether given element is present in tuple or not. For example x = (1, 2, 3, 4, 5) and we need to find if 3 is present in tuple so that in this case resultant output should be True.Using in Operatorin operator checks if an element is present in a tuple by
2 min read