Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Similar index pairs in Tuple lists

Python – Similar index pairs in Tuple lists

Sometimes, while working with Python tuples, we can have a problem in which we need to perform similar index pairing. This kind of problem is peculiar, but can occur across certain domains. Let’s discuss certain way in which this task can be performed.

Input : test_list1 = [(5, ), (1, ), (8, ), (10, )] test_list2 = [(8, ), (1, ), (11, ), (9, )] 
Output : [[(5, 8)], [(1, 1)], [(8, 11)], [(10, 9)]] 

Input : test_list1 = [(5, 6, 7, 6)] test_list2 = [(8, 6, 7, 9)] 
Output : [[(5, 8), (6, 6), (7, 7), (6, 9)]]

Method 1: Using list comprehension + zip() The combination of above functions can be used to solve this problem. In this, we perform the task of zipping similar index elements using zip() and list comprehension is used to compile all the pairs. 

Python3




# Python3 code to demonstrate working of
# Similar index pairs in Tuple lists
# Using list comprehension + zip()
 
# initializing lists
test_list1 = [(5, 6), (1, 2), (8, 9), (10, 33)]
test_list2 = [(8, 7), (1, 3), (11, 23), (9, 4)]
 
# printing original list
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Similar index pairs in Tuple lists
# Using list comprehension + zip()
res = [list(zip(a, b)) for a, b in zip(test_list1, test_list2)]
 
# printing result
print("The paired tuples : " + str(res))


Output : 

The original list 1 is : [(5, 6), (1, 2), (8, 9), (10, 33)]
The original list 2 is : [(8, 7), (1, 3), (11, 23), (9, 4)]
The paired tuples : [[(5, 8), (6, 7)], [(1, 1), (2, 3)], [(8, 11), (9, 23)], [(10, 9), (33, 4)]]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. The list comprehension + zip() is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n), the algorithm uses an additional list to store the result, thus consuming linear space which is O(n).

Method 2: Using for loop

Explanation:

  • Initialize two lists test_list1 and test_list2 as given in the problem.
  • Print the original lists using the print() function.
  • Initialize an empty list result that will contain the paired tuples.
  • Using a nested for loop, iterate over each index of test_list1 and extract corresponding elements from test_list1 and test_list2.
  • Create a tuple of the extracted elements and append it to a temporary list temp.
  • Append the temporary list temp to the result list.
  • Finally, print the result list containing the paired tuples.

Python3




# initializing lists
test_list1 = [(5, 6), (1, 2), (8, 9), (10, 33)]
test_list2 = [(8, 7), (1, 3), (11, 23), (9, 4)]
 
# printing original list
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Similar index pairs in Tuple lists
result = []
for i in range(len(test_list1)):
    temp = []
    for j in range(len(test_list1[i])):
        temp.append((test_list1[i][j], test_list2[i][j]))
    result.append(temp)
 
# printing result
print("The paired tuples : " + str(result))


Output

The original list 1 is : [(5, 6), (1, 2), (8, 9), (10, 33)]
The original list 2 is : [(8, 7), (1, 3), (11, 23), (9, 4)]
The paired tuples : [[(5, 8), (6, 7)], [(1, 1), (2, 3)], [(8, 11), (9, 23)], [(10, 9), (33, 4)]]

Time complexity: O(n^2), where n is the length of the input lists.
Auxiliary space: O(n^2), since we are creating a nested list to store the paired tuples.

Method 3: Using reduce:

Algorithm:

  1. Initialize an empty list called ‘result’.
  2. For each index ‘i’ in range from 0 to the length of ‘test_list1’:
    a. Initialize an empty list called ‘temp’.
    b. For each index ‘j’ in range from 0 to the length of sub-list at index ‘i’ of ‘test_list1’:
    i. Create a tuple of elements at index ‘j’ of sub-list at index ‘i’ of ‘test_list1’ and sub-list at index ‘i’ of ‘test_list2’, respectively.
    ii. Append the created tuple to ‘temp’.
    c. Append ‘temp’ to ‘result’.
  3. Print the final list ‘result’.
     

Python3




from functools import reduce
 
# initializing lists
test_list1 = [(5, 6), (1, 2), (8, 9), (10, 33)]
test_list2 = [(8, 7), (1, 3), (11, 23), (9, 4)]
 
# print original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# using reduce to get paired tuples
result = reduce(lambda acc, curr: acc + [[(curr[0][i], curr[1][i]) for i in range(len(curr[0]))]], zip(test_list1, test_list2), [])
 
# print result
print("The paired tuples : " + str(result))
#This code is contributed by Rayudu.


Output

The original list 1 is : [(5, 6), (1, 2), (8, 9), (10, 33)]
The original list 2 is : [(8, 7), (1, 3), (11, 23), (9, 4)]
The paired tuples : [[(5, 8), (6, 7)], [(1, 1), (2, 3)], [(8, 11), (9, 23)], [(10, 9), (33, 4)]]

Time Complexity: O(n^2), where ‘n’ is the length of each sublist in ‘test_list1’ and ‘test_list2’, since there are nested loops iterating over each element in both sublists.

Space Complexity: O(n^2), where ‘n’ is the length of each sublist in ‘test_list1’ and ‘test_list2’, since the result list is a nested list containing tuples of the same size as the sublists in the input lists.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments