Open In App

Replace index elements with elements in Other List-Python

Last Updated : 30 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of replacing index elements with elements from another list involves mapping the indices from one list to the corresponding elements in a second list. For each index in the first list, the element at that index is retrieved from the second list and stored in a new result list.

For example, given two lists, a = [‘Gfg’, ‘is’, ‘best’] and b = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0], the task is to generate a new list where each element in list b serves as an index to fetch values from list a. For each index in b, the corresponding value from a is selected and placed into the new result list. In this case, the resulting list will be [‘Gfg’, ‘is’, ‘best’, ‘is’, ‘Gfg’, ‘Gfg’, ‘Gfg’, ‘best’, ‘is’, ‘is’, ‘best’, ‘Gfg’] .

Using list comprehension

List comprehension provides a efficient way to replace index elements with elements from another list. By iterating through the index list and directly accessing the corresponding elements from the main list, this method offers high readability and performance. It’s widely used due to its simplicity and the fact that it eliminates the need for additional function calls or explicit loops.

Python
a = ['Gfg', 'is', 'best'] # list of strings
b = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # list of indices

res = [a[idx] for idx in b]
print(res)

Output
['Gfg', 'is', 'best', 'is', 'Gfg', 'Gfg', 'Gfg', 'best', 'is', 'is', 'best', 'Gfg']

Explanation: list comprehension iterate through list b and replace each index with the corresponding element from list a. For each index in b, the corresponding element from a is accessed and added to the result list res.

Using map()

map() applies a given function to each item of an iterable and returns a map object which can be converted to a list. This method allows replacing index elements with those in another list by defining a function often a lambda function to perform the lookup. While efficient for larger datasets, it introduces some overhead with function calls compared to list comprehension.

Python
a = ['Gfg', 'is', 'best'] # list of strings
b = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] # list of indices

res = list(map(lambda idx: a[idx], b))
print(res)

Output
['Gfg', 'is', 'best', 'is', 'Gfg', 'Gfg', 'Gfg', 'best', 'is', 'is', 'best', 'Gfg']

Explanation: map() with a lambda function to iterate over list b and fetch elements from list a based on the indices in b. The result is converted to a list and stored in res, which contains the elements from a in the order specified by b.

Using for loop

For loop iterates over the index list and appends the corresponding elements from the main list to a new list. While easy to understand and flexible, this method is less efficient than list comprehension because of the explicit iteration and the use of the append() which adds some additional overhead.

Python
a = ['Gfg', 'is', 'best']
b = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0]

res = []
for idx in b:
    res.append(a[idx])
print(res)

Output
['Gfg', 'is', 'best', 'is', 'Gfg', 'Gfg', 'Gfg', 'best', 'is', 'is', 'best', 'Gfg']

Explanation: for loop iterate over the list b, which contains the indices. For each index in b, the corresponding element from list a is accessed using a[idx] and appended to the result list res. Finally, the list res is printed, which contains the elements from a ordered according to the indices in b.

Using numpy array

When dealing with larger datasets or arrays, numpy provides a powerful alternative for replacing index elements. It allows us to treat lists as arrays and perform efficient element-wise operations. The method is ideal when working with numerical or large-scale data, offering faster performance and better memory management compared to lists, although it may be considered overkill for simpler cases.

Python
import numpy as np

a = ['Gfg', 'is', 'best']
b = [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0]

a = np.array(a)
res = a[b]
print(res)

Output
['Gfg' 'is' 'best' 'is' 'Gfg' 'Gfg' 'Gfg' 'best' 'is' 'is' 'best' 'Gfg']

Explanation: list a is converted to a numpy array and elements are accessed using the indices in list b with a[b]. This returns the elements from a in the order specified by b.



Next Article
Practice Tags :

Similar Reads