Python Splice List

Lists are linear data structures used to store sequential data in Python. In this article, we will discuss how to splice a list into another list in Python.

What Is List Splicing in Python?

The list splicing operation in Python is used to concatenate two or more lists. It is almost similar to string splicing in python except that we splice lists instead of strings. You can also insert a list in between the elements of another list using splicing. The syntax for list splicing in python will be as follows.

myList= list1+list2+list3

Here, list1, list2, and list3 are the smaller lists and myList is the bigger one created by splicing all the smaller lists.

Python Splice List

To splice two or more lists in Python, you can use the + operator as shown below.

list1=[1,2,3]
list2=[4,5,6]
list3= [7,8,9]
myList=list1+list2+list3
print("The first list is:",list1)
print("The second list is:",list2)
print("The third list is:",list3)
print("The spliced list is:",myList)

Output:

The first list is: [1, 2, 3]
The second list is: [4, 5, 6]
The third list is: [7, 8, 9]
The spliced list is: [1, 2, 3, 4, 5, 6, 7, 8, 9]

In the above example, we first create three lists namely list1, list2, and list3. Next, we have used the concatenation operator + to splice the lists to create myList.

Splice Two Parts of a List in Python

If you are given two parts of a list, you can splice them using the concatenation operation as shown below.

first_part=[1,2,3]
second_part=[4,5,6]
myList=first_part+second_part
print("The first list is:",first_part)
print("The second list is:",second_part)
print("The spliced list is:",myList)

Output:

The first list is: [1, 2, 3]
The second list is: [4, 5, 6]
The spliced list is: [1, 2, 3, 4, 5, 6]

This example is similar to the previous example. Here, we have taken two lists namely first_part and second_part, and combined them to create a larger list named myList.

Splicing a List at the End of Another List

To splice a list say list2 at the end of another list list1, we can append list2 to the end of list1 using the + operator as shown below.

list1=[1,2,3]
list2=[4,5,6]
myList=list1+list2
print("The original list is:",list1)
print("The input list is:",list2)
print("The spliced list is:",myList)

Output:

The original list is: [1, 2, 3]
The input list is: [4, 5, 6]
The spliced list is: [1, 2, 3, 4, 5, 6]

Splice a List at the Start of Another List

Instead of concatenating at the end of a list, We can also splice a list at the start of another list as shown below.

list1=[1,2,3]
list2=[4,5,6]
myList=list2+list1
print("The original list is:",list1)
print("The input list is:",list2)
print("The spliced list is:",myList)

Output:

The original list is: [1, 2, 3]
The input list is: [4, 5, 6]
The spliced list is: [4, 5, 6, 1, 2, 3]

In this example, we have spliced list2 at the start of list1. This operation is similar to splicing list1 at the end of list2.

Splice a List at a Given Element in Python

To splice a list at a given element, we will use the following steps.

  • Consider that we have to splice a new list named list2 into another list list1 at index splice_index.
  • For this, we will first split list1 at the index splice_index into two parts using python indexing operator.
  • The first part contains elements from the start of the list till index splice_index-1. The second part of list1 contains elements from index splice_index till the end.
  • After obtaining the sub-lists, we will splice both parts with list2 to create a larger list named myList. For this, we will concatenate the first part of list1, the second part of list1, and list2 such as list2 is sandwiched between the first part of list1 and the second part of list1 using the + operator.

After execution of the above logic, we can splice list2 into list1 at the index splice_index as shown below.

list1=[1,2,3,4,5,6]
list2= [7,8,9]
splice_index=list1.index(4)
first_part=list1[:splice_index]
second_part=list1[splice_index:]
myList=first_part+list2+second_part
print("The original list is:",list1)
print("The input list is:",list2)
print("The spliced list is:",myList)

Output:

The original list is: [1, 2, 3, 4, 5, 6]
The input list is: [7, 8, 9]
The spliced list is: [1, 2, 3, 7, 8, 9, 4, 5, 6]

Splice a List by Replacing a Given Element

In the above example, we have spliced a new list into an existing list at a given index. In this process, no elements are removed from the original list. To replace the element at any index splice_index with a new list while list splicing, we can use the following steps.

  • First, we will split the original list list1 into three parts. The first part contains elements from the start of the list till index splice_index-1. The second part contains the element at index splice_index and the third part contains the elements from index splice_index+1 till the end of the list. 
  • After splitting the list list1 into three parts, we can splice the new list list2 between the first and the third part of the original list.
  • After this, the output list has all the elements from the original list and the new list except the element at index splice_index of the original list.

You can observe this in the following example.

list1=[1,2,3,4,5,6]
list2= [7,8,9]
splice_index=list1.index(4)
first_part=list1[:splice_index]
second_part=list1[splice_index:splice_index+1]
third_part=list1[splice_index+1:]
myList=first_part+list2+third_part
print("The original list is:",list1)
print("The input list is:",list2)
print("The spliced list is:",myList)

Output:

The original list is: [1, 2, 3, 4, 5, 6]
The input list is: [7, 8, 9]
The spliced list is: [1, 2, 3, 7, 8, 9, 5, 6]

Splicing a List by Replacing Two or More Elements

Instead of replacing a single element, you can also splice a new list into another list by replacing two or more elements from the original list. For instance, if we have to splice a new list into a given list by replacing elements from the index i to index j, we will use the following steps.

  • First, we will split the original list list1 into three parts. The first part contains elements from the start of the list till index i-1. The second part contains the element from index i to index j. The third part contains the elements from index j+1 till the end of the list. 
  • After splitting the list list1 into three parts, we can splice the new list list2 between the first and the third part of list1. After this, the output list has all the elements from list1 and list2 except for the element at the index i to j of the list1.

You can observe this in the following example.

list1=[1,2,3,4,5,6]
list2= [7,8,9]
splice_index=list1.index(3)
first_part=list1[:splice_index]
second_part=list1[splice_index:splice_index+3]
third_part=list1[splice_index+3:]
myList=first_part+list2+third_part
print("The original list is:",list1)
print("The input list is:",list2)
print("The spliced list is:",myList)

Output:

The original list is: [1, 2, 3, 4, 5, 6]
The input list is: [7, 8, 9]
The spliced list is: [1, 2, 7, 8, 9, 6]

Conclusion

In this article, we discussed different ways to splice a list into another list in Python. I hope you enjoyed reading this article.

To learn more about python programming, you can read this article on python simplehttpserver. You might also like this article on ValueError : Too Many Values to Unpack Error in Python.

Stay tuned for more informative articles.

Happy Learning!

Donate to Avid Python

Dear reader,
If you found this article helpful and informative, I would greatly appreciate your support in keeping this blog running by making a donation. Your contributions help us continue creating valuable content for you and others who come across my blog. 
No matter how big or small, every donation is a way of saying "thank you" and shows that you value the time and effort we put into writing these articles. If you feel that our article has provided value to you, We would be grateful for any amount you choose to donate. 
Thank you for your support! 
Best regards, 
Aditya
Founder

If you want to Pay Using UPI, you can also scan the following QR code.

Payment QR Code
Payment QR Code

Leave a Reply