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.
Split A List in Python Using Its Length
In python, you have the len() method to get the size of the list. Using this method once you get the size of the list you can easily split the list in the different ways you want. In the below example code we are splitting the list into two equal parts from the middle of the list.
#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]
Using List Comprehension Method to Split the List
This operation can be accomplished by combining the capabilities of list comprehension and zip(). In this, we zip the start and the end of the list and then continue splitting the list as new lists enter and slicing them off. Let us see that in the below code in detail how we can achieve that.
Note: This works only in Python 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.
Using numpy To Split List in N Equal Parts
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 List with Elements givenList = [1,2,3,4,5,6,7,8] #Slicing the List 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.
The language does not include a built-in function to accomplish this, therefore in this article, we’ll look at how to divide a list into even portions using the Python programming language.
That is for how to split a list in python. 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.