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
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
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
Python - Replace sublist from Initial element Given a list and replacement sublist, perform the replacement of list sublist on basis of initial element of replacement sublist. Input : test_list = [3, 7, 5, 3], repl_list = [7, 8] Output : [3, 7, 8, 3] Explanation : Replacement starts at 7 hence 7 and 8 are replaced. Input : test_list = [3, 7, 5,
7 min read
Remove common elements from two list in Python When working with two lists in Python, we may need to remove the common elements between them. A practical example could be clearing out overlapping tasks between two to-do lists. The most efficient way to remove common elements between two lists is by using sets. Pythona = [1, 2, 3, 4, 5] b = [4, 5
3 min read
Remove Multiple Elements from List in Python In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list.Pythona = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remove = [20, 40,
2 min read
Accessing all elements at given list of indexes-Python Sometimes, you may have a list of data and a separate list of indexes and the goal is to extract only the elements at those specific positions. For example, given a list [10, 20, 30, 40, 50] and a list of indexes [1, 3, 4], you want to retrieve [20, 40, 50] the values at those index positions in the
2 min read