Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Join Tuples to Integers in Tuple List

Python – Join Tuples to Integers in Tuple List

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.


Output

The 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).

RELATED ARTICLES

Most Popular

Recent Comments