In this tutorial, you will learn how to split a list in Python. As you may encounter in using some of the data science or machine learning algorithms where you need to split a list to extract the values out of it.
Splitting a list based on chunk size is possible. The result of splitting a list into n equal parts is another list, this time with the same number of list items. Some lists will have one more element than others if n is not evenly divided by the length of the list being split.
1. Split A List into Two Half Using Its Length in Python
You can use the len() method in python to get the list’s size. As soon as you know the size of your list, you can easily divide it into any number of different sections. The code below shows how to divide a list into two equal parts from the middle of the list.
You will need the below things to split the list using Python.
- Get the Length of the List using size() method.
- Divide the Length of the List into Half and save it in variable as middle index
- Use this middle index variable to split the list into two half.
#Initializing the List to get the it split givenList = [1,2,3,4,5,6,7,8,9,10] #getting the length of the list using len() sizeOfList = len(givenList) #Printing the List in using size from the middle #By Splitting the list in two equal sizes midIndex = int(sizeOfList/2) print(givenList[:midIndex]) print(givenList[midIndex:])
[1, 2, 3, 4, 5] [6, 7, 8, 9, 10]
1.1 Split A List At Index
If you want to split a List at a particular index then that is really easy to perform. If you know the index and length of the list then all you are required to use is the index to separate the list into two chunks. The method used here is similar to what I have used for splitting the list into two equal half.
Let us see in the below code how you can split a list at index using Python.
#Python Code Example #Initializing the List to get the it split givenList = [1,2,3,4,5,6,7,8,9,10] #getting the length of the list using len() sizeOfList = len(givenList) #suppose you want to split from index 3 indexToSplit = 3 #Splitting List from a Index newList =  newList.append(givenList[:indexToSplit]) newList.append(givenList[indexToSplit:]) print(newList)
[[1, 2, 3], [4, 5, 6, 7, 8, 9, 10]]
As you can see from the above code, I wanted to split the list from index 3 and I was able to do it using the index slicing method in Python. And I saved both the split lists into a new list and printed it out as output.
2. Using List Comprehension Method To Split List by Indexes
This operation can be accomplished by combining the capabilities of list comprehension and zip(). Zip the beginning and end of the list together, and then continue slicing the list off as new lists enter. Let us see in the below code in detail how we can achieve that.
Note: This works only in Python Version 3.x and above.
# Using the List Comprehension + Zip() # To Split the List in Python 3 and above # Intializing the List of Numbers givenList = [8,9,10,11,12,13,14,15,16,17,18,19,20] # Listing the Index where you want Spliting # To End split_list = [3, 5, 7] # Print the Original List before Splitting print ("The original list is : " + str(givenList)) # With Help of List comprehension + zip() # Performing Custom Split splittedList = [givenList[i : j] for i, j in zip( + split_list, split_list + [None])] # printing result print ("The splitted lists are : " + str(splittedList))
The original list is : [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] The splitted lists are : [[8, 9, 10], [11, 12], [13, 14], [15, 16, 17, 18, 19, 20]]
As in the above code, you see that we have used the list of indexes through which we want to split our given list. Using this split index method you can easily separate the list in equal or any amount of chunks you want.
2.1 Split A List By Value – List Comprehension Method
The other variation to the above problem can be split a list by value. If you have a list where there is a value keep repeating and you want to split the list as soon as that value is found. This can be achieved easily using the list comprehension method as we discussed above.
Let us see in the below example code how to split the list by value in Python using the List comprehension method.
#Python Code Example # Using the List Comprehension + Zip() # To Split the List in Python 3 and above # Intializing the List of Numbers givenList = [8,9,10,11,12,13,14,15,11,16,17,18,11,19,11,20] # Getting Index of Values to Split splitListByValue = [indx for indx in enumerate(givenList) if indx==11] # Print the Original List before Splitting print ("The original list is : " + str(givenList)) # With Help of List comprehension + zip() # Performing Custom Split splittedList = [givenList[i : j] for i, j in zip( + splitListByValue, splitListByValue + [None])] # printing result print ("The splitted lists are : " + str(splittedList))
The original list is : [8, 9, 10, 11, 12, 13, 14, 15, 11, 16, 17, 18, 11, 19, 11, 20] The splitted lists are : [[8, 9, 10], [11, 12, 13, 14, 15], [11, 16, 17, 18], [11, 19], [11, 20]]
First I have created the list of indexes that has the value of 11 to it and saved it to the list. Now using these indexes that are equal to the value, the list was split by the value present in the list at a particular index.
3. Using numpy Array – Split List Into Multiple List
Python’s numpy.array split() function can be used to divide a list into n pieces. The array is divided into numerous sub-arrays using the np.split() function. A list of nearly equal-sized Numpy arrays is returned by the array split() method of the package.
Let us see the example in the below code for using the NumPy library to split a list in Python into equal parts.
#Importing the Numpy Library to #Get the array split method import numpy as npLib #Initializing the Array with Elements givenList = [1,2,3,4,5,6,7,8] #Slicing the Array using array Split splits = npLib.array_split(givenList, 4) #Printing the Splitted List for splittedList in splits: print(list(splittedList))
String splitting and list splitting are popular programming tasks in Python and other computer languages. Sometimes we must divide our data in unusual ways, but more often than not, we must divide it into even parts.
I hope you were able to understand how to split a list in Python as I have listed three different types of methods that you can use to solve this problem statement.
Please let me know if you have any suggestions or comments related to the above information or if you got any more methods to do so, we will be happy to include them in our post.
Please follow us on Facebook and Twitter. We will be soon coming with Free Courses on Python on all the topics such as Beginner, Intermediate, and Advance. Keep following us for further updates. Also, signup for the below newsletter to keep posted once we launch the courses.