Replace index elements with elements in Other List-Python
Last Updated : 30 Jan, 2025
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.
Similar Reads
Python | Replace elements in second list with index of same element in first list Given two lists of strings, where first list contains all elements of second list, the task is to replace every element in second list with index of elements in first list. Method #1: Using Iteration Python3 # Python code to replace every element # in second list with index of first element. # List
5 min read
Python | Replace list elements with its ordinal number Given a list of lists, write a Python program to replace the values in the inner lists with their ordinal values. Examples:Input : [[1, 2, 3], [ 4, 5, 6], [ 7, 8, 9, 10]]Output : [[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]Input : [['a'], [ 'd', 'e', 'b', 't'], [ 'x', 'l']]Output : [[0], [1, 1, 1, 1], [2, 2
5 min read
Python program to remove duplicate elements index from other list Given two lists, the task is to write a Python program to remove all the index elements from 2nd list which are duplicate element indices from 1st list. Examples: Input : test_list1 = [3, 5, 6, 5, 3, 7, 8, 6], test_list2 = [1, 7, 6, 3, 7, 9, 10, 11] Output : [1, 7, 6, 9, 10] Explanation : 3, 7 and 1
7 min read
Remove elements at Indices in List - Python In Python, lists store items in a specific order and each item has an index. Removing elements by index means creating a new list that excludes items at certain positions. This helps in keeping only the needed elements while discarding others based on their index. For example:Input : li= [5, 6, 3, 7
2 min read
Python | Equate two list index elements Sometimes we need to link two list from the point of view of their index elements and this kind of problem comes mostly in places where we need to display in formatted form the linkage of two lists with one another. This a very specific problem, but can be useful whenever we need a possible solution
8 min read
Python - All replacement combination from other list Given a list, the task is to write a Python program to perform all possible replacements from other lists to the current list. Input : test_list = [4, 1, 5], repl_list = [8, 10]Â Output : [(4, 1, 5), (4, 1, 8), (4, 1, 10), (4, 5, 8), (4, 5, 10), (4, 8, 10), (1, 5, 8), (1, 5, 10), (1, 8, 10), (5, 8, 1
3 min read