Python Program to Reorder a List According to Given Indexes

In the previous article, we have discussed Python Program to Find Volume and Surface Area of a Cone
Given two lists of equal length i.e. one is the list of elements and the other of indexes list, the task is to reorder the given list according to the given indexes.

For example:

let Given List = [1, 2, 3, 4, 5]

Given Indexes List = [2, 0, 3, 4, 1]

The given list after reordering according to the given indexes list = [2 5 1 3 4]

The Index list after reordering = [0 1 2 3 4]

Examples:

Example1:

Input:

Given List = [1, 2, 3, 4, 5]
Given Index List = [3, 4, 0, 2, 1]

Output:

The given list after reordering according to the given indexes list :
3 5 4 1 2 
The Index list after reordering:
0 1 2 3

Example2:

Input:

Given List =  [10, 20, 30, 40, 50, 60]
Given Index List = [4, 3, 5, 0, 1, 2]

Output:

The given list after reordering according to the given indexes list :
40 50 60 20 10 30 
The Index list after reordering:
0 1 2 3 4 5

Program to Reorder an Array/List According to Given Indexes in Python

Below are the ways to reorder the given list according to the given indexes.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Give the list of indexes as static input and store it in another variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a list and assign all the elements with 0 of the length of the given list and store it in another variable say “tempry”.
  • Loop from 0 to length of the given list using the for loop.
  • Assign the givenindexlist[itertor value] as index and givenlist[iterator] as value to tempry list.
  • Loop from 0 to length of the given list using the for loop.
  • Assign the given list at the iterator with the “tempry” list at the iterator value.
  • Assign the given list at the iterator index with iterator value.
  • Loop from 0 to length of the given list using the for loop.
  • Inside the loop, Print the iterator value of the given list.
  • Loop from 0 to length of the given index list using the for loop.
  • Inside the loop, Print the iterator value of the given index list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [9, 8, 2, 1]
# Give the list of indexes as static input and store it in another variable.
gvn_indxlst = [1, 3, 0, 2]
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_gvn_lst = len(gvn_lst)
# Take a list and assign all the elements with 0 of the length of the given list and
# store it in another variable say "tempry".
tempry = [0] * len_gvn_lst
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
 # Assign the givenindexlist[itertor value] as index and givenlist[iteratr] as value
    # to temp list
    tempry[gvn_indxlst[itror]] = gvn_lst[itror]
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
 # Assign the given list at the iterator with the "tempry" list at iterator value
    gvn_lst[itror] = tempry[itror]
 # Assign the given list at itertor index with iterator value
    gvn_indxlst[itror] = itror
print("The given list after reordering according to the given indexes list :")
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
  # Inside the loop, Print the iterator value of the given list.
    print(gvn_lst[itror], end=" ")
print()
print("The Index list after reordering:")
# Loop from 0 to length of the given index list using the for loop.
for itror in range(0, len_gvn_lst):
  # Inside the loop, Print the iterator value of the given index list.
    print(gvn_indxlst[itror], end=" ")

Output:

The given list after reordering according to the given indexes list :
2 9 1 8 
The Index list after reordering:
0 1 2 3

Method #2: Using For loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the list of indexes as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a list and assign all the elements with 0 of the length of the given list and store it in another variable say “tempry”.
  • Loop from 0 to length of the given list using the for loop.
  • Assign the givenindexlist[itertor value] as index and givenlist[iterator] as value to tempry list.
  • Loop from 0 to length of the given list using the for loop.
  • Assign the given list at the iterator with the “tempry” list at the iterator value.
  • Assign the given list at the iterator index with iterator value.
  • Loop from 0 to length of the given list using the for loop.
  • Inside the loop, Print the iterator value of the given list.
  • Loop from 0 to length of the given index list using the for loop.
  • Inside the loop, Print the iterator value of the given index list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = givnlst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the list of indexes as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
gvn_indxlst = givnlst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_gvn_lst = len(gvn_lst)
# Take a list and assign all the elements with 0 of the length of the given list and
# store it in another variable say "tempry".
tempry = [0] * len_gvn_lst
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
 # Assign the givenindexlist[itertor value] as index and givenlist[iteratr] as value
    # to temp list
    tempry[gvn_indxlst[itror]] = gvn_lst[itror]
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
 # Assign the given list at the iterator with the "tempry" list at iterator value
    gvn_lst[itror] = tempry[itror]
 # Assign the given list at itertor index with iterator value
    gvn_indxlst[itror] = itror
print("The given list after reordering according to the given indexes list: ")
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
  # Inside the loop, Print the iterator value of the given list.
    print(gvn_lst[itror], end=" ")
print()
print("The Index list after reordering:")
# Loop from 0 to length of the given index list using the for loop.
for itror in range(0, len_gvn_lst):
  # Inside the loop, Print the iterator value of the given index list.
    print(gvn_indxlst[itror], end=" ")

Output:

Enter some random List Elements separated by spaces = 6 4 3 2 1
Enter some random List Elements separated by spaces = 4 0 1 3 2
The given list after reordering according to the given indexes list: 
4 3 1 2 6 
The Index list after reordering:
0 1 2 3 4

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.