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 - Pair elements with Rear element in Matrix Row
Sometimes, while working with data, we can have a problem in which we need to pair each element in container to a specific index element, like rear element. This kind of problem can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using lis
7 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
Python - Remove elements at Indices in List
Given List, remove all the elements present in the indices list in Python. Input : test_list = [5, 6, 3, 7, 8, 1, 2, 10], idx_list = [2, 4, 5]Â Output : [5, 6, 7, 2, 10]Â Explanation : 3, 6, and 1 has been removed. Input : test_list = [5, 6, 3, 7, 8, 1, 2, 10], idx_list = [2]Â Output : [5, 6, 7, 8,
7 min read
Python | Convert column to separate elements in list of lists
There are instances in which we might require to extract a particular column of a Matrix and assign its each value as separate entity in list and this generally has a utility in Machine Learning domain. Let's discuss certain ways in which this action can be performed.Method #1 : Using list slicing a
4 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 - Extracting Priority Elements in Tuple List
Sometimes, while working with Python Records, we can have a problem in which we need to perform extraction of all the priority elements from records, which usually occur as one of the binary element tuple. This kind of problem can have possible application in web development and gaming domains. Let'
5 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
Python - Replace Elements greater than K
Given element list, replace all elements greater than K with given replace character. Input : test_list = [3, 4, 7, 5, 6, 7], K = 5, repl_chr = None Output : [3, 4, None, 5, None, None] Explanation : Characters are replaced by None, greater than 5. Input : test_list = [3, 4, 7, 5, 6, 7], K = 4, repl
4 min read