Given a list of numbers of list, write a Python program to create a list of tuples having first element as the number and second element as the cube of the number.
Example:
Input: list = [1, 2, 3] Output: [(1, 1), (2, 8), (3, 27)] Input: list = [9, 5, 6] Output: [(9, 729), (5, 125), (6, 216)]
Method #1 : Using pow() function.We can use list comprehension to create a list of tuples. The first element will be simply an element and second element will be cube of that number. Below is the Python implementation:
Python3
# Python program to create a list of tuples # from given list having number and # its cube in each tuple # creating a list list1 = [ 1 , 2 , 5 , 6 ] # using list comprehension to iterate each # values in list and create a tuple as specified res = [(val, pow (val, 3 )) for val in list1] # print the result print (res) |
Output:
[(1, 1), (2, 8), (5, 125), (6, 216)]
Time complexity: O(n), where n is the length of list1.
Auxiliary space: O(n), since we are creating a new list of tuples with the same length as list1.
Method #2: Using ** operator
Python3
# Python program to create a list of tuples # from given list having number and # its cube in each tuple # creating a list list1 = [ 1 , 2 , 5 , 6 ] # using list comprehension to iterate each # values in list and create a tuple as specified res = [(val, val * * 3 ) for val in list1] # print the result print (res) |
[(1, 1), (2, 8), (5, 125), (6, 216)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Method #3: Using map() and lambda function
We can also use the map() function along with a lambda function to create a list of tuples. The lambda function will take an element from the list as input and return a tuple containing the element and its cube as output. The map() function will apply this lambda function to all elements in the list and return a list of tuples.
Here is the Python implementation of this approach:
Python3
list1 = [ 1 , 2 , 5 , 6 ] res = list ( map ( lambda x: (x, x * * 3 ), list1)) print (res) #This code is contributed by Edula Vinay Kumar Reddy |
[(1, 1), (2, 8), (5, 125), (6, 216)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), since a new list of tuples is created with the same size as the input list.
Method 4 : using a for loop to iterate through the values in the list and create a tuple of each value and its cube.
Step-by-step approach:
- Create an empty list named res to store the result.
- Use a for loop to iterate over each value in list1.
- Inside the for loop, create a tuple named tup that contains the current value from list1 and its cube (val**3).
- Append the tup tuple to the res list.
- After the for loop completes, print the res list that contains tuples with each value from list1 and its cube.
Below is the implementation of the above approach:
Python3
# creating a list list1 = [ 1 , 2 , 5 , 6 ] # creating an empty list to store the result res = [] # iterating through each value in the list for val in list1: # creating a tuple of the value and its cube tup = (val, val * * 3 ) # adding the tuple to the result list res.append(tup) # print the result print (res) |
[(1, 1), (2, 8), (5, 125), (6, 216)]
Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(n), where n is the number of elements in the list, since we are creating a new list to store the result.
METHOD 5:Using re method
APPROACH:
This Python program creates a list of tuples where each tuple contains a number and its cube from a given string. The string contains a list of numbers separated by commas.
ALGORITHM:
1.Import the re module for regular expression operations.
2.Initialize an input string containing the list of numbers.
3.Use the re.findall() method to extract all the numbers from the input string and convert them to integers using a list comprehension.
4.Use a list comprehension to create a new list of tuples where each tuple contains a number and its cube.
5.Print the resulting list of tuples.
Python3
import re lst_str = "1, 2, 5, 6" lst = [ int (num) for num in re.findall(r '\d+' , lst_str)] result = [(num, num * * 3 ) for num in lst] print (result) |
[(1, 1), (2, 8), (5, 125), (6, 216)]
Time Complexity:
The time complexity of this program is O(n) where n is the number of elements in the input list. The time complexity of re.findall() method is O(n) where n is the length of the input string. The time complexity of the list comprehension for creating a new list is also O(n).
Space Complexity:
The space complexity of this program is also O(n) where n is the number of elements in the input list. This is because the program creates two lists: one for the extracted numbers and another for the resulting list of tuples. The space required for these two lists grows linearly with the size of the input list.