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 to itself.
Example:
Python3
# Initializing listl = [1, 2, 3, 4] # Cube List using loopres = []for i in l: res.append(i*i*i) # printing resultprint(res) |
Output:
[1, 8, 27, 64]
Method 2 : Using pow() function
This is also the brute force way. In this, we use in-built pow() function
Example:
Python3
# Initializing listl = [1, 2, 3, 4] # Cube List using loopres = []for i in l: res.append(pow(i, 3)) # printing resultprint(res) |
Output:
[1, 8, 27, 64]
Method 3 : 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 listl = [1, 2, 3, 4] # Cube List using list comprehensionres = [pow(i, 3) for i in l] # printing resultprint(res) |
Output:
[1, 8, 27, 64]
Method 4: Using lambda
This can also be achieved using lambda function
Example:
Python3
# Initializing listl = [1, 2, 3, 4] res = list(map(lambda x: x ** 3, l))print(res) |
Output:
[1, 8, 27, 64]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


