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.
3 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 t
3 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 | 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
Python - Add list elements to tuples list
Sometimes, while working with Python tuples, we can have a problem in which we need to add all the elements of a particular list to all tuples of a list. This kind of problem can come in domains such as web development and day-day programming. Let's discuss certain ways in which this task can be don
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]
4 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 | Return new list on element insertion
The usual append method adds the new element in the original sequence and does not return any value. But sometimes we require to have a new list each time we add a new element to the list. This kind of problem is common in web development. Let's discuss certain ways in which this task can be perform
5 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 - Alternate List elements
Given 2 lists, print the element in zig-zag manner, i.e print similar indices of lists and then proceed to next. Input : test_list1 = [5, 3, 1], test_list2 = [6, 4, 2] Output : [5, 6, 3, 4, 1, 2, 4] Explanation : 5 and 6, as in 0th index are printed first, then 3 and 4 on 1st index, and so on. Input
4 min read