Open In App

Python | Move one list element to another list

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.  
Auxiliary Space: O(n), where n is the number of elements in the new res list 

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.

Method 4 :  Using the del statement and the append() method. 

Here are the steps:

Initialize the two lists test_list1 and test_list2.
Print the original lists.
Find the index of the element to move in test_list2 using the index() method.
Remove the element from test_list2 using the del statement.
Append the element to test_list1 using the append() method.
Print the updated lists.

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)
del test_list2[index]
test_list1.append(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, 3, 8, 10]
The list 2 after remove is : [7, 6, 3, 8, 12]

Time complexity: O(n), where n is the length of test_list2 since we need to find the index of the element to move.

Auxiliary space: O(1), since we are only using a constant amount of extra memory to store the element and index.



Last Updated : 05 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads