Move One List Element to Another List - Python
Last Updated :
10 Feb, 2025
The task of moving one list element to another in Python involves locating a specific element in the source list, removing it, and inserting it into the target list at a desired position. For example, if a = [4, 5, 6, 7, 3, 8] and b = [7, 6, 3, 8, 10, 12], moving 10 from b to index 4 in a results in a = [4, 5, 6, 7, 10, 3, 8] and b = [7, 6, 3, 8, 12].
Using list comprehension
This method efficiently moves an element from one list to another in a single step using list comprehension. It minimizes function calls and optimizes performance by finding and inserting the element in a compact and readable way. This approach is ideal for quick modifications in small to medium-sized lists.
Python
a = [4, 5, 6, 7, 3, 8]
b = [7, 6, 3, 8, 10, 12]
[a.insert(4, b.pop(idx)) for idx in [b.index(10)]]
print(a) # After insert
print(b) # After removal
Output[4, 5, 6, 7, 10, 3, 8]
[7, 6, 3, 8, 12]
Explanation: b.pop(idx) removes the element 10 from list b and a.insert(4, b.pop(idx)) inserts this removed element at index 4 in list a, effectively moving 10 from b to a.
Using pop() and insert()
A direct method where pop is used to remove an element from one list and insert places it into another. This approach ensures clarity while maintaining efficiency. It is widely used for moving elements between lists without unnecessary complexity.
Python
a = [4, 5, 6, 7, 3, 8]
b = [7, 6, 3, 8, 10, 12]
idx = b.index(10)
val = b.pop(idx)
a.insert(4, val)
print(a)
print(b)
Output[4, 5, 6, 7, 10, 3, 8]
[7, 6, 3, 8, 12]
Explanation: This code first finds the index of the element 10 in list b using b.index(10), storing it in idx. Then, b.pop(idx) removes 10 from b and stores it in val. Finally, a.insert(4, val) inserts 10 at index 4 in list a, effectively moving the element from b to a.
Using filter()
filter() method quickly finds the element’s index before removing and inserting it into the target list. By avoiding unnecessary iterations, it enhances efficiency, making it a good choice for handling large lists where performance is a concern.
Python
a = [4, 5, 6, 7, 3, 8]
b = [7, 6, 3, 8, 10, 12]
idx = next(filter(lambda i: b[i] == 10, range(len(b))))
a.insert(4, b.pop(idx))
print(a)
print(b)
Output[4, 5, 6, 7, 10, 3, 8]
[7, 6, 3, 8, 12]
Explanation: filter() iterates over b's indices, finding where b[i] == 10 and next retrieves the first matching index as idx. Then, b.pop(idx) removes 10 from b and a.insert(4, b.pop(idx)) places it at index 4 in a, completing the transfer.
Using loop
A simple but less efficient method where a loop finds the element, removes it and inserts it into another list. While not the fastest, it offers flexibility for handling custom conditions when moving elements between lists.
Python
a = [4, 5, 6, 7, 3, 8]
b = [7, 6, 3, 8, 10, 12]
for i in range(len(b)):
if b[i] == 10:
a.insert(4, b.pop(i))
break
print(a)
print(b)
Output[4, 5, 6, 7, 10, 3, 8]
[7, 6, 3, 8, 12]
Explanation: loop iterates through b
, finds 10
, removes it using b.pop(i)
and inserts it at index 4
in a
. The break
ensures only the first occurrence is moved efficiently.
Similar Reads
Python - Operation to each element in list Given a list, there are often when performing a specific operation on each element is necessary. While using loops is a straightforward approach, Python provides several concise and efficient methods to achieve this. In this article, we will explore different operations for each element in the list.
2 min read
Python - Move Element to End of the List We are given a list we need to move particular element to end to the list. For example, we are given a list a=[1,2,3,4,5] we need to move element 3 at the end of the list so that output list becomes [1, 2, 4, 5, 3].Using remove() and append()We can remove the element from its current position and th
3 min read
Python | Move given element to List Start The conventional problem involving the element shifts has been discussed many times earlier, but sometimes we have strict constraints performing them, and knowledge of any possible variation helps. This article talks about one such problem of shifting Kâs at the start of the list, catch here is it c
6 min read
Insert list in another list - Python We are given two lists, and our task is to insert the elements of the second list into the first list at a specific position. For example, given the lists a = [1, 2, 3, 4] and b = [5, 6], we want to insert list 'b' into 'a' at a certain position, resulting in the combined list a = [1, 2, 5, 6, 3, 4]
3 min read
Python | Assign range of elements to List Assigning elements to list is a common problem and many varieties of it have been discussed in the previous articles. This particular article discusses the insertion of range of elements in the list. Let's discuss certain ways in which this problem can be solved. Method #1 : Using extend() This can
5 min read