Open In App

Python program to create a list of tuples from given list having number and its cube in each tuple

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)


Output

[(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


Output

[(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)


Output

[(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)


Output

[(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.



Previous Article
Next Article

Similar Reads

Python | Remove tuples having duplicate first value from given list of tuples
Given a list of tuples, the task is to remove all tuples having duplicate first values from the given list of tuples. Examples: Input: [(12.121, 'Tuple1'), (12.121, 'Tuple2'), (12.121, 'Tuple3'), (923232.2323, 'Tuple4')] Output: [(12.121, 'Tuple1'), (923232.2323, 'Tuple4')]Input: [('Tuple1', 121), ('Tuple2', 125), ('Tuple1', 135), ('Tuple4', 478)]
7 min read
Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
The task is to write a Python Program to sort a list of tuples in increasing order by the last element in each tuple. Input: [(1, 3), (3, 2), (2, 1)] Output: [(2, 1), (3, 2), (1, 3)] Explanation: sort tuple based on the last digit of each tuple. Methods #1: Using sorted(). Sorted() method sorts a list and always returns a list with the elements in
7 min read
Python | Reverse each tuple in a list of tuples
Given a list of tuples, write a Python program to reverse each tuple in the given list of tuples. Examples: Input : [(1, 2), (3, 4, 5), (6, 7, 8, 9)] Output : [(2, 1), (5, 4, 3), (9, 8, 7, 6)] Input : [('a', 'b'), ('x', 'y'), ('m', 'n')] Output : [('b', 'a'), ('y', 'x'), ('n', 'm')] Method #1 : Negative-step slicing We can use standard negative-ste
5 min read
Python Program to Convert Tuple Value List to List of Tuples
Given a dictionary with values as a tuple list, convert it to a key-mapped list of tuples. Input : test_dict = {'Gfg' : [(5, ), (6, )], 'is' : [(5, )], 'best' :[(7, )]} Output : [('Gfg', 5), ('Gfg', 6), ('is', 5), ('best', 7)] Explanation : Keys grouped with values.Convert Tuple Value List to List of Tuples Using loop + * operator + items() This is
8 min read
Python program to find Tuples with positive elements in List of tuples
Given a list of tuples. The task is to get all the tuples that have all positive elements. Examples: Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)] Output : [(4, 5, 9)] Explanation : Extracted tuples with all positive elements. Input : test_list = [(-4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)] Output : [] Explanation : No tuple wit
10 min read
Python program to find tuples which have all elements divisible by K from a list of tuples
Given a list of tuples. The task is to extract all tuples which have all elements divisible by K. Input : test_list = [(6, 24, 12), (60, 12, 6), (12, 18, 21)], K = 6 Output : [(6, 24, 12), (60, 12, 6)] Explanation : Both tuples have all elements multiple of 6. Input : test_list = [(6, 24, 12), (60, 10, 5), (12, 18, 21)], K = 5 Output : [(60, 10, 5)
7 min read
Python | Find the tuples containing the given element from a list of tuples
Given a list of tuples, the task is to find all those tuples containing the given element, say n. Examples: Input: n = 11, list = [(11, 22), (33, 55), (55, 77), (11, 44)] Output: [(11, 22), (11, 44)] Input: n = 3, list = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)] Output: [(14, 3), (1, 3), (3, 3)] There are multiple ways we can find the tuples contai
6 min read
Python - Filter all uppercase characters Tuples from given list of tuples
Given a Tuple list, filter tuples that contain all uppercase characters. Input : test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")] Output : [('GFG', 'IS', 'BEST')] Explanation : Only 1 tuple has all uppercase Strings. Input : test_list = [("GFG", "iS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")] Output : [
8 min read
Percentage increase in volume of the cube if a side of cube is increased by a given percentage
Given here is a cube, whose one side is increased by a given percentage. The task is to find percentage increase in the volume of the cube.Examples: Input: x = 10 Output: 33.1% Input: x = 50 Output: 237.5% Approach In a cube, all sides are equal, so, length = breadth = heightlet side of the cube = agiven percentage increase = x%so, volume before in
3 min read
Python | Remove duplicate tuples from list of tuples
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x')] Method #1 : List comprehension This is a naive
5 min read