Example:
tuple = ("python", "includehelp", 43, 54.23)
Creating a list of tuples from given list having number and its cube in each tuple
We have a list of elements and we need to create another list of elements such that each element of the new list is a tuple. And each of the tuples consists of two values one the element from the list and the second will be the cube of the value.
Example:
Input:
list = [4, 1, 6, 2]
Output:
[(4, 64), (1, 1), (6, 216), (2, 8)]
We simply need to iterate over all the elements of the list and then for each element create a tuple consisting of the element and its cube and then append it to a list.
This can be done by simply loop and also to shorten the code we can use comprehension techniques. Here is a code depicting both the methods.
Program:
Output:
Using comprehension
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer