Python - Join Tuples to Integers in Tuple List
Last Updated :
14 Mar, 2023
Sometimes, while working with Python records, we can have a problem in which we need to concatenate all the elements, in order, to convert elements in tuples in List to integer. This kind of problem can have applications in many domains such as day-day and competitive programming. Let's discuss certain ways in which this task can be performed.
Input : test_list = [(4, 5, 6), (5, 1), (1, 3, 8, 0), (6, 9)]
Output : [456, 51, 1380, 69]
Input : test_list = [(4, 5, 6, 8, 9)]
Output : [45689]
Method #1 : Using loop This is brute force method in which this task can be performed. In this, we perform join of all the elements using number creation mathematically compute the result.
Python3
# Python3 code to demonstrate working of
# Join Tuples to Integers in Tuple List
# Using loop
# helpr_fnc
def join_tup(tup):
res = tup[0]
for idx in tup[1:]:
res = res * 10 + idx
return res
# initializing list
test_list = [(4, 5), (5, 6), (1, 3), (6, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Join Tuples to Integers in Tuple List
# Using loop
res = [join_tup(idx) for idx in test_list]
# printing result
print("The joined result : " + str(res))
Output : The original list is : [(4, 5), (5, 6), (1, 3), (6, 9)]
The joined result : [45, 56, 13, 69]
Time complexity: O(nk), where n is the number of tuples in the list and k is the number of elements in each tuple.
Auxiliary space: O(1), as only constant extra space is used for the variables.
Method #2 : Using map() + join() + int() The combination of above functions can be used to solve the problem. In this, we perform concatenation by string conversion, joining using join() and int() is used to convert result back to integer.
Python3
# Python3 code to demonstrate working of
# Join Tuples to Integers in Tuple List
# Using map() + join() + int()
# initializing list
test_list = [(4, 5), (5, 6), (1, 3), (6, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Join Tuples to Integers in Tuple List
# Using map() + join() + int()
res = [int(''.join(map(str, idx))) for idx in test_list]
# printing result
print("The joined result : " + str(res))
Output : The original list is : [(4, 5), (5, 6), (1, 3), (6, 9)]
The joined result : [45, 56, 13, 69]
Time Complexity: O(n*k) where where n is the number of tuples in the list and k is the number of elements in each tuple. The map() + join() + int() is used to perform the task and it takes O(n*k) time.
Auxiliary space: O(1), as only constant extra space is used.
Method #3 :Using reduce() and lambda function:
Algorithm:
1.Import the reduce() function from the functools module.
2.Initialize a list of tuples test_list.
3.Apply the map() function to the list of tuples, with a lambda function that takes each tuple as its argument and applies the reduce() function to 4.join the elements of the tuple to form an integer.
5.The reduce() function multiplies the current element by 10 and adds the next element to it, effectively joining the tuple.
6.The resulting integer is returned by the lambda function and added to the res list by map().
7.Print the res list.
Python3
# import reduce function from functools module
from functools import reduce
# initialize list of tuples
test_list = [(4, 5), (5, 6), (1, 3), (6, 9)]
# printing original list
print("The original list is : " + str(test_list))
# apply reduce and lambda function to join tuples to integers in tuple list
# reduce() applies a function to each element of the sequence (tuples in this case) and returns a single value (integer)
# lambda function takes two arguments (x and y) and returns their product, which is then added to x and returned
res = list(map(lambda tup: reduce(lambda x, y: x * 10 + y, tup), test_list))
# print the result
print("The joined result : " + str(res))
#This code is contributed by Jyothi pinjala.
OutputThe original list is : [(4, 5), (5, 6), (1, 3), (6, 9)]
The joined result : [45, 56, 13, 69]
Time Complexity: The time complexity of this code is O(nm), where n is the number of tuples in test_list and m is the maximum number of elements in a tuple. The reduce() function is applied to each tuple, which takes O(m) time to join the elements of the tuple to form an integer.
Auxiliary Space: The space complexity of this code is O(n), where n is the number of tuples in test_list. The res list is the only additional data structure used in this code, which has a space complexity of O(n).
Similar Reads
Python | Combining tuples in list of tuples Sometimes, we might have to perform certain problems related to tuples in which we need to segregate the tuple elements to combine with each element of complex tuple element( such as list ). This can have application in situations we need to combine values to form a whole. Let's discuss certain ways
7 min read
Flatten tuple of List to tuple - Python The task of flattening a tuple of lists to a tuple in Python involves extracting and combining elements from multiple lists within a tuple into a single flattened tuple. For example, given tup = ([5, 6], [6, 7, 8, 9], [3]), the goal is to flatten it into (5, 6, 6, 7, 8, 9, 3). Using itertools.chain(
3 min read
Swap tuple elements in list of tuples - Python The task of swapping tuple elements in a list of tuples in Python involves exchanging the positions of elements within each tuple while maintaining the list structure. Given a list of tuples, the goal is to swap the first and second elements in every tuple. For example, with a = [(3, 4), (6, 5), (7,
3 min read
Python | Convert Integral list to tuple list Sometimes, while working with data, we can have a problem in which we need to perform type of interconversions of data. There can be a problem in which we may need to convert integral list elements to single element tuples. Let's discuss certain ways in which this task can be performed. Method #1 :
3 min read
Convert List to Tuple in Python The task of converting a list to a tuple in Python involves transforming a mutable data structure list into an immutable one tuple. Using tuple()The most straightforward and efficient method to convert a list into a tuple is by using the built-in tuple(). This method directly takes any iterable like
2 min read