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 code to demonstrate working of # Join Tuples to Integers in Tuple List# Using loop # helpr_fncdef join_tup(tup): res = tup[0] for idx in tup[1:]: res = res * 10 + idx return res # initializing listtest_list = [(4, 5), (5, 6), (1, 3), (6, 9)] # printing original listprint("The original list is : " + str(test_list)) # Join Tuples to Integers in Tuple List# Using loopres = [join_tup(idx) for idx in test_list] # printing result print("The joined result : " + str(res)) |
The original list is : [(4, 5), (5, 6), (1, 3), (6, 9)] The joined result : [45, 56, 13, 69]
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 code to demonstrate working of # Join Tuples to Integers in Tuple List# Using map() + join() + int() # initializing listtest_list = [(4, 5), (5, 6), (1, 3), (6, 9)] # printing original listprint("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)) |
The original list is : [(4, 5), (5, 6), (1, 3), (6, 9)] The joined result : [45, 56, 13, 69]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


