Python | Insert list in another list
The problem of inserting a number at any index is a quite common one. But sometimes we require to insert the whole list into another list. These kinds of problems occur in Machine Learning while playing with data. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using insert()
+ loop
In this method, we insert one element by 1 at a time using the insert function. This way we add all the list elements at the specified index in other list.
# Python3 code to demonstrate # to insert one list in another # using insert() + loop # initializing lists test_list = [ 4 , 5 , 6 , 3 , 9 ] insert_list = [ 2 , 3 ] # initializing position pos = 2 # printing original list print ( "The original list is : " + str (test_list)) # printing insert list print ( "The list to be inserted is : " + str (insert_list)) # using insert() + loop # to insert one list in another for i in range ( len (insert_list)): test_list.insert(i + pos, insert_list[i]) # printing result print ( "The list after insertion is : " + str (test_list)) |
Output :
The original list is : [4, 5, 6, 3, 9] The list to be inserted is : [2, 3] The list after insertion is : [4, 5, 2, 3, 6, 3, 9]
Method #2 : Using list slicing
This is the most pythonic and elegant way to perform this particular task. In this method, we just slice the list where we need to add the element and assign the list to be inserted.
# Python3 code to demonstrate # to insert one list in another # using list slicing # initializing lists test_list = [ 4 , 5 , 6 , 3 , 9 ] insert_list = [ 2 , 3 ] # initializing position pos = 2 # printing original list print ( "The original list is : " + str (test_list)) # printing insert list print ( "The list to be inserted is : " + str (insert_list)) # using list slicing # to insert one list in another test_list[pos:pos] = insert_list # printing result print ( "The list after insertion is : " + str (test_list)) |
Output :
The original list is : [4, 5, 6, 3, 9] The list to be inserted is : [2, 3] The list after insertion is : [4, 5, 2, 3, 6, 3, 9]
Recommended Posts:
- Python list | insert()
- Python | Insert after every Nth element in a list
- Python | Insert value after each k letters in given list of string
- Python program to insert an element into sorted list
- Python | Insert the string at the beginning of all items in a list
- Python | Split given list and insert in excel file
- List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert mixed data types tuple list to string list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Pair and combine nested list to tuple list
- Python | Replace elements in second list with index of same element in first list
- Python | Convert string List to Nested Character List
- Python | Add list elements with a multi-list based on index
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.