Dark Mode On/Off
PUBLISHED ON: AUGUST 12, 2021

# Create a list of tuples from given list having number and its cube in each tuple

In this tutorial, you will learn to write a Python program where we have to create a list of tuples from a given list of numbers having the number and its cube in the tuple.

A tuple is a sequence of objects. The tuple created from a given list of numbers will have two elements where the first element will be the number and the second element will be the cube of that number.

Look at the examples to understand the input and output format.

Input: [1, 3, 2]

Output: [(1,1), (3, 27), (2, 8)]

To solve this problem in Python, we can use the list comprehension method. List comprehension is a shortened syntax for creating a new list based on the values of an existing list. We can create a new list of tuples with the number and its cube.

## Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function to create a list of tuples

Step 2- Initialise a result list to store the tuples

Step 3- Using list comprehension create a list of tuples, where the number and its cube are one pair

Step 4- Return this list

Step 5- Declare a list of numbers and pass it in the function

Step 6- Print the value returned by the function

## Python Program

In this program, we have defined a function that will create a list of tuples, where each tuple will have the number and its cube. To get the cube we have used (**) operator. Which is another way of using exponents. We can also use` pow() `method to get the cubes of the number.

``````# list of tuples having number and its cube

def cubeoflist(li):
# list of tuples
result=[(num, num**3) for num in li]
return result

# initialise list
li = [3, 4, 1, 2]

# print the result
print(cubeoflist(li))``````

[(3, 27), (4, 64), (1, 1), (2, 8)]

## Conclusion

In this tutorial, we have learned to create a list of tuples having the number and its cube in the tuple, from a given list of numbers. We have calculated cubes of the numbers in the list and using list comprehension created a new list with tuples as elements.

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.