Friday, October 10, 2025
HomeLanguagesPython – Rearrange elements second index greater than first

Python – Rearrange elements second index greater than first

Given 2 lists, for a given index, 2nd list element is always larger than first, and if not, we rearrange it.

Input : test_list1 = [36, 38, 40, 132], test_list2 = [35, 37, 39, 41, 133] 
Output : [37, 39, 41, 133] 
Explanation : Each element in result list is greater than its index counterpart of 1st list. (Eg. 37 > 36) 

Input : test_list1 = [2, 6], test_list2 = [5, 3, 8] 
Output : [5, 8] 
Explanation : Here 5 > 2 and 8 > 6.

Method 1: Using loop This is brute way to tackle this problem. In this, we try to get best suitable next higher element after the whole list traversal and perform the necessary rearrangement. 

Python3




# Python3 code to demonstrate working of
# Rearrange elements second index greater than first
# Using loop
 
# initializing lists
test_list1 = [14, 16, 18, 110]
test_list2 = [13, 15, 17, 19, 111]
 
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Rearrange elements second index greater than first
# Using loop
x = y = 0
res1, res2 = [], []
while x < len(test_list2) and y < len(test_list1):
     
    # checking for greater element
    if test_list2[x] > test_list1[y]:
        res2.append(test_list2[x])
        res1.append(test_list1[y])
        while y < len(test_list1) and test_list2[x] > test_list1[y]:
            res1[-1] = test_list1[y]
            y += 1
    x += 1
 
# printing result
print("List 2 after conversion : " + str(res2))


Output : 

The original list 1 is : [14, 16, 18, 110]
The original list 2 is : [13, 15, 17, 19, 111]
List 2 after conversion : [15, 17, 19, 111]

Time complexity: O(n), where n is the length of test_list1 + test_list2.
Auxiliary Space: O(n), where n is the length of test_list1 + test_list2. The additional space is used to store the rearranged lists res1 and res2.

Method 2: Using list comprehension

Python3




# Python3 code to demonstrate working of
# Rearrange elements second index greater than first
# Using list comprehensions
 
# initializing lists
test_list1 = [14, 16, 18, 110]
test_list2 = [13, 15, 17, 19, 111]
 
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Rearranging elements in list 2 such that the second index is greater than the first
x = 0
res2 = [test_list2[i] for i in range(len(test_list2)) if x < len(test_list1) and test_list2[i] > test_list1[x]]
x = [i for i in range(len(test_list1)) if x < len(test_list2) and test_list2[x] > test_list1[i]]
 
# printing result
print("List 2 after conversion : " + str(res2))
#This code is contributed by Vinay pinjala.


Output

The original list 1 is : [14, 16, 18, 110]
The original list 2 is : [13, 15, 17, 19, 111]
List 2 after conversion : [15, 17, 19, 111]

Time complexity: O(n)
Auxiliary Space: O(n)

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

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7100 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS