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 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,
5 min read
Python - Combine list with other list elements
Given two lists, combine list with each element of the other list. Examples: Input : test_list = [3, 5, 7], pair_list = ['Gfg', 'is', 'best'] Output : [([3, 5, 7], 'Gfg'), ([3, 5, 7], 'is'), ([3, 5, 7], 'best')] Explanation : All lists paired with each element from other list. Input : test_list = [3
6 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
Split Elements of a List in Python
Splitting elements of a list in Python means dividing strings inside each list item based on a delimiter. split() Method is the easy and most straightforward method to split each element is by using the split() method. This method divides a string into a list based on a delimiter. Here's how we can
2 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 | List Element Count with Order
Sometimes, while working with lists or numbers we can have a problem in which we need to attach with each element of list, a number, which is the position of that element's occurrence in that list. This type of problem can come across many domains. Let's discuss a way in which this problem can be so
4 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
Remove an Element from a List by Index in Python
We are given a list and an index value. We have to remove an element from a list that is present at that index value in a list in Python. In this article, we will see how we can remove an element from a list by using the index value in Python. Example: Input: [1, 2, 3, 4, 5], index = 2 Output: [1, 2
3 min read