Open In App

Python | Arrange Tuples consecutively in list

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with tuple list, we may require a case in which we require that a tuple starts from the end of previous tuple, i.e the element 0 of every tuple should be equal to ending element of tuple in list of tuple. This type of problem and sorting is useful in competitive programming. Let’s discuss way in which this problem can be solved. 

Method : Using loop + dict() This task can easily be solved by converting a tuple container list to dictionary and then it’s easy to access a value of a key and arrange them accordingly to sort in a way in which one tuple element begins with the ending of other element. 

Python3




# Python3 code to demonstrate working of
# Arranging Tuples consecutively in list
# using loop + dict()
 
# initialize list
test_list = [(5, 6), (11, 8), (6, 11), (8, 9) ]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Arranging Tuples consecutively in list
# using loop + dict()
temp = dict(test_list) 
ele = test_list[0][0
res = []
for _ in range(len(test_list)):
    res.append((ele, temp[ele]))
    ele = temp[ele]
 
# printing result
print("The arranged list is : " + str(res))


Output : 

The original list is : [(5, 6), (11, 8), (6, 11), (8, 9)]
The arranged list is : [(5, 6), (6, 11), (11, 8), (8, 9)]

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list 

Method #2:Using the sorting and then comparing the values:

  • Initialize the list of tuples.
  • Sort the list based on the second element of each tuple.
  • Loop through the list and compare each tuple with the next tuple.
  • If the tuples are consecutive, continue to the next iteration of the loop.
  • If the tuples are not consecutive, loop through the remaining tuples to find the tuple whose first element is equal to the second element of the current tuple, and swap it with the next tuple.
  • Print the arranged list.

Python3




# initialize list
test_list = [(5, 6), (11, 8), (6, 11), (8, 9)]
 
# printing original list
print("The original list is:", test_list)
 
# Arranging Tuples consecutively in list
# using sorting and comparing
arranged_list = sorted(test_list, key=lambda x: x[1])
for i in range(len(arranged_list)-1):
   if arranged_list[i][1] == arranged_list[i+1][0]:
       continue
   else:
       for j in range(i+1, len(arranged_list)):
           if arranged_list[i][1] == arranged_list[j][0]:
               arranged_list[i+1], arranged_list[j] = arranged_list[j], arranged_list[i+1]
               break
 
# printing result
print("The arranged list is:", arranged_list)


Output

The original list is: [(5, 6), (11, 8), (6, 11), (8, 9)]
The arranged list is: [(5, 6), (6, 11), (11, 8), (8, 9)]

Time complexity is O(n^2) in the worst case due to the nested loop

Auxiliary Space is O(n) because we use a sorted copy of the original list



Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads