Python program to construct Equidigit tuples
Last Updated :
26 Apr, 2023
Given the Elements list, divide the tuple list into similar digit tuples pairs.
Input : test_list = [5654, 223, 982143, 34, 1021]
Output : [(56, 54), (2, 23), (982, 143), (3, 4), (10, 21)]
Explanation : Element in tuples equidistributed.
Input : test_list = [5654, 223, 1021]
Output : [(56, 54), (2, 23), (10, 21)]
Explanation : Element in tuples equidistributed.
Method #1 : Using loop + slicing + str()
In this, we perform the task of dividing by getting mid-idx and then slice from mid, initially integral number is split to a string by using str().
Python3
# Python3 code to demonstrate working of
# Construct Equidigit tuples
# Using loop + slicing str()
# initializing list
test_list = [5654, 223, 982143, 34, 1021]
# printing original list
print("The original list is : " + str(test_list))
res = []
for sub in test_list:
# getting mid element
mid_idx = len(str(sub)) // 2
# slicing Equidigits
el1 = str(sub)[:mid_idx]
el2 = str(sub)[mid_idx:]
res.append((int(el1), int(el2)))
# printing result
print("Equidigit tuples List : " + str(res))
Output:
The original list is : [5654, 223, 982143, 34, 1021] Equidigit tuples List : [(56, 54), (2, 23), (982, 143), (3, 4), (10, 21)]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using list comprehension + divmod() function
Ue a list comprehension to iterate over the list of integers and construct the equidigit tuples using the divmod() function. The divmod() function takes two arguments and returns a tuple containing the quotient and remainder of the division. We can use this function to split an integer into its two halves.
Step-by-step approach:
- Initialize the list of integers test_list with the given values.
- Print the original list using the print() function and string concatenation.
- Use a list comprehension to iterate over the list of integers test_list.
- In each iteration, compute the length of the integer using the len() function and integer division // by 2. This gives us the index of the middle digit.
- Use the divmod() function to split the integer into its two halves. The first argument to divmod() is the integer, and the second argument is the power of 10 corresponding to the index of the middle digit.
- The list comprehension returns a list of tuples containing the two halves of each integer.
- Print the resulting list of tuples using the print() function and string concatenation.
Python3
# Python3 code to demonstrate working of
# Construct Equidigit tuples
# Using list comprehension + divmod() function
# initializing list
test_list = [5654, 223, 982143, 34, 1021]
# printing original list
print("The original list is : " + str(test_list))
# using list comprehension + divmod() function
res = [divmod(sub, 10 ** (len(str(sub)) // 2)) for sub in test_list]
# printing result
print("Equidigit tuples List : " + str(res))
OutputThe original list is : [5654, 223, 982143, 34, 1021]
Equidigit tuples List : [(56, 54), (22, 3), (982, 143), (3, 4), (10, 21)]
Time complexity: O(n*log(m)), where n is the length of the input list and m is the maximum number of digits in any integer in the list.
Auxiliary space: O(n), since we are creating a new list of tuples containing the equidigit halves.
Similar Reads
Python | Convert Tuple to integer Sometimes, while working with records, we can have a problem in which we need to convert the data records to integer by joining them. Let's discuss certain ways in which this task can be performed. Method #1 : Using reduce() + lambda The combination of above functions can be used to perform this tas
5 min read
tuple() Constructor in Python In Python, the tuple() constructor is a built-in function that is used to create tuple objects. A tuple is similar to a list, but it is immutable (elements can not be changed after creating tuples). You can use the tuple() constructor to create an empty tuple, or convert an iterable (such as a list,
2 min read
Output of Python Programs | Set 18 (List and Tuples) 1) What is the output of the following program? PYTHON L = list('123456') L[0] = L[5] = 0 L[3] = L[-2] print(L) a) [0, '2', '3', '4', '5', 0] b) ['6', '2', '3', '5', '5', '6'] c) ['0', '2', '3', '5', '5', '0'] d) [0, '2', '3', '5', '5', 0] Ans. (d) Explanation: L[0] is '1' and L[5] is '6', both of t
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
Convert Dictionary to List of Tuples - Python Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2
3 min read