Python | Insert Nth element to Kth element in other list

Sometimes, while working with Python list, there can be a problem in which we need to perform the inter list shifts of elements. Having 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 code to demonstrate working of ` `# Insert Nth element to Kth element in other 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)) ` ` `  `# initializing N  ` `N ``=` `5` ` `  `# initializing K  ` `K ``=` `3` ` `  `# Using pop() + index() + insert() ` `# Insert Nth element to Kth element in other list ` `res ``=` `test_list1.insert(K, test_list2.pop(N)) ` ` `  `# 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, 12, 7, 3, 8]
The list 2 after remove is : [7, 6, 3, 8, 10]
```
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.