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) |
[1, 8, 27, 64]
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) |
[1, 8, 27, 64]
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) |
[1, 8, 27, 64]
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) |
[1, 8, 27, 64]
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.