Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Rearrange list by other list order

Python – Rearrange list by other list order

Sometimes, while working with Python lists, we can have problem in which we need to perform sorting of lists according to other list ordering. This can have applications in many domains including day-day programming and school programming. Lets discuss certain ways in which this task can be performed. 

Method #1: Using List Comprehension This task can be performed using list comprehension. In this, we iterate the sort order list and simply append if list is present in target list. The indices are similar to the sort order list. 

Step-by-step approach:

  • Two lists test_list1 and test_list2 are initialized with some elements.
  • The original test_list1 and test_list2 are printed using the print() function and str() function to convert the lists to strings.
  • The list comprehension is used to rearrange the test_list1 based on the order of test_list2. The comprehension works by iterating over each element of test_list1 and only keeping the elements that also exist in test_list2. The resulting list is stored in res.
  • The rearranged list res is printed using the print() function and str() function to convert the list to a string.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Rearrange list by other list order
# Using list comprehension
 
# initializing lists
test_list1 = [5, 6, 7, 4, 8, 9, 2]
test_list2 = [9, 6, 4]
 
# printing original list
print("The original list 1 is : " + str(test_list1))
 
# printing original list
print("The original list 2 is : " + str(test_list2))
 
# Rearrange list by other list order
# Using list comprehension
res = [ele for ele in test_list1 if ele in test_list2]
 
# printing result
print("The list after sorting is : " + str(res))


Output

The original list 1 is : [5, 6, 7, 4, 8, 9, 2]
The original list 2 is : [9, 6, 4]
The list after sorting is : [6, 4, 9]

Time Complexity: O(n) where n is the total number of values in the list “test_list”. 
Auxiliary Space: O(n) where n is the total number of values in the list “test_list”. 

  Method #2 : Using sorted + index() The combination of above functions can also be used to perform this task. In this we use sort function with index as key to sort according to other list. 

Python3




# Python3 code to demonstrate working of
# Rearrange list by other list order
# Using sorted + index()
 
# initializing lists
test_list1 = [5, 6, 7, 4, 8, 9, 2]
test_list2 = [9, 6, 4]
 
# printing original list
print("The original list 1 is : " + str(test_list1))
 
# printing original list
print("The original list 2 is : " + str(test_list2))
 
# Rearrange list by other list order
# Using sorted + index()
res = sorted(test_list2, key = test_list1.index)
 
# printing result
print("The list after sorting is : " + str(res))


Output

The original list 1 is : [5, 6, 7, 4, 8, 9, 2]
The original list 2 is : [9, 6, 4]
The list after sorting is : [6, 4, 9]

Time Complexity: O(nlogn), where n is the length of the input list. This is because we’re using the using sorted + index() which has a time complexity of O(nlogn) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.

Method 3: Using a loop and a temporary list

Step-by-step approach:

  • Create an empty list called res.
  • Iterate over each value in test_list1.
  • If the value is also in test_list2, append it to res.
  • Return res.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Rearrange list by other list order
# Using a loop and a temporary list
 
# initializing lists
test_list1 = [5, 6, 7, 4, 8, 9, 2]
test_list2 = [9, 6, 4]
 
# printing original list
print("The original list 1 is : " + str(test_list1))
 
# printing original list
print("The original list 2 is : " + str(test_list2))
 
# Rearrange list by other list order
# Using a loop and a temporary list
res = []
for val in test_list1:
    if val in test_list2:
        res.append(val)
 
# printing result
print("The list after sorting is : " + str(res))


Output

The original list 1 is : [5, 6, 7, 4, 8, 9, 2]
The original list 2 is : [9, 6, 4]
The list after sorting is : [6, 4, 9]

Time complexity: O(n), where n is the length of test_list1.
Auxiliary space: O(n), where n is the length of test_list1.

Method 4: Using the filter() function and a lambda function

In this method, we use the filter() function that filters the elements from the first list that satisfy the condition specified in the lambda function. The lambda function checks if each element is present in the second list using the in operator.

Python3




# Python3 code to demonstrate working of
# Rearrange list by other list order
# Using the filter() function and a lambda function
 
# initializing lists
test_list1 = [5, 6, 7, 4, 8, 9, 2]
test_list2 = [9, 6, 4]
 
# printing original list
print("The original list 1 is : " + str(test_list1))
 
# printing original list
print("The original list 2 is : " + str(test_list2))
 
# Rearrange list by other list order
# Using the filter() function and a lambda function
res = list(filter(lambda x: x in test_list2, test_list1))
 
# printing result
print("The list after sorting is : " + str(res))


Output:

The original list 1 is : [5, 6, 7, 4, 8, 9, 2]
The original list 2 is : [9, 6, 4]
The list after sorting is : [6, 4, 9]

Time complexity: O(n^2), where n is the length of the first list. 
Auxiliary space: O(n) since we are creating a new list to store the rearranged elements.

RELATED ARTICLES

Most Popular

Recent Comments