Python program to construct Equidigit tuples

PythonServer Side ProgrammingProgramming

When it is required to construct equi-digit tuples, the ‘//’ operator and the list slicing is used.

Example

Below is a demonstration of the same

my_list = [5613, 1223, 966143, 890, 65, 10221]

print("The list is :")
print(my_list)

my_result = []
for sub in my_list:

   mid_index = len(str(sub)) // 2
   element_1 = str(sub)[:mid_index]
   element_2 = str(sub)[mid_index:]

   my_result.append((int(element_1), int(element_2)))

print("The resultant list is :")
print(my_result)

Output

The list is :
[5613, 1223, 966143, 890, 65, 10221]
The resultant list is :
[(56, 13), (12, 23), (966, 143), (8, 90), (6, 5), (10, 221)]

Explanation

  • A list is defined and is displayed on the console.

  • An empty list is defined.

  • The list is iterated over and the middle index is obtained.

  • The list slicing is used to obtained values before middle index and after middle index.

  • These elements are assigned to two variables.

  • These are appended to the empty list by converting them to integers.

  • This is displayed as output on the console.

raja
Published on 14-Sep-2021 11:23:36
Advertisements