The Wayback Machine - https://web.archive.org/web/20230304052551/https://www.geeksforgeeks.org/python-move-one-list-element-to-another-list/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Move one list element to another list

Improve Article
Save Article
  • Last Updated : 28 Feb, 2023
Improve Article
Save Article

Sometimes, while working with Python list, there can be a problem in which we need to perform the inter list shifts of elements. Having a solution to this problem is always very useful. Let’s discuss certain way in which this task can be performed. 

Method : Using pop() + insert() + index() This particular task can be performed using combination of above functions. In this we just use the property of pop function to return and remove element and insert it to the specific position of other list using index function. 

Python3




# Python3 code to demonstrate working of
# Move one list element to another list
# Using pop() + index() + insert()
 
# initializing lists
test_list1 = [4, 5, 6, 7, 3, 8]
test_list2 = [7, 6, 3, 8, 10, 12]
 
# printing original lists
print("The original list 1 is : " +  str(test_list1))
print("The original list 2 is : " +  str(test_list2))
 
# Using pop() + index() + insert()
# Move one list element to another list
res = test_list1.insert(4, test_list2.pop(test_list2.index(10)))
 
# Printing result
print("The list 1 after insert is : " +  str(test_list1))
print("The list 2 after remove is : " +  str(test_list2))

Output : 

The original list 1 is : [4, 5, 6, 7, 3, 8]
The original list 2 is : [7, 6, 3, 8, 10, 12]
The list 1 after insert is : [4, 5, 6, 7, 10, 3, 8]
The list 2 after remove is : [7, 6, 3, 8, 12]

The approach used in this code involves the following steps:

  • Finding the index of the element in the source list (test_list2) that you want to move to the target list (test_list1) using the index method.
  • Removing the element from the source list using the remove method.
  • Inserting the removed element into the target list using the insert method and passing in the index of the element found in step 1 as an argument.

Python3




# initializing lists
test_list1 = [4, 5, 6, 7, 3, 8]
test_list2 = [7, 6, 3, 8, 10, 12]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Moving an element from one list to another
element = 10
index = test_list2.index(element)
test_list1 = test_list1[:index] + [element]+test_list1[index:]
test_list2.remove(element)
 
# Printing result
print("The list 1 after insert is : " + str(test_list1))
print("The list 2 after remove is : " + str(test_list2))

Output

The original list 1 is : [4, 5, 6, 7, 3, 8]
The original list 2 is : [7, 6, 3, 8, 10, 12]
The list 1 after insert is : [4, 5, 6, 7, 10, 3, 8]
The list 2 after remove is : [7, 6, 3, 8, 12]

Time complexity: O(n)

Auxiliary Space: O(1)

Method#3: Using Slicing and Concatenation.

Algorithm:

  1. Identify the element to be moved from one list to another.
  2. Get the index of the element in the list it currently belongs to.
  3. Remove the element from the original list using the index obtained in step 2.
  4. Insert the element into the new list at the index obtained in step 2.

Python3




# initializing lists
test_list1 = [4, 5, 6, 7, 3, 8]
test_list2 = [7, 6, 3, 8, 10, 12]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Moving an element from one list to another
element = 10
index = test_list2.index(element)
test_list1 = test_list1[:index] + [element]+test_list1[index:]
test_list2.remove(element)
 
# Printing result
print("The list 1 after insert is : " + str(test_list1))
print("The list 2 after remove is : " + str(test_list2))

Output

The original list 1 is : [4, 5, 6, 7, 3, 8]
The original list 2 is : [7, 6, 3, 8, 10, 12]
The list 1 after insert is : [4, 5, 6, 7, 10, 3, 8]
The list 2 after remove is : [7, 6, 3, 8, 12]

The time complexity of this algorithm depends on the implementation. In the given implementation, finding the index of the element in the second list has a time complexity of O(n), where n is the length of the list. Removing the element from the second list has a time complexity of O(n) as well, as it requires searching for the element in the list. Inserting the element into the first list has a time complexity of O(n) as well, as it requires shifting all the elements after the index one position to the right. Therefore, the overall time complexity of this implementation is O(n).

The auxiliary space of this implementation is O(n), as it requires creating a new list by concatenating three lists, two of which have n elements each.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!