Python: Split a List into n Chunks (4 Ways) • datagy
datagy.io › python-split-list-into-chunksSep 21, 2021 · Let’s see how we can use numpy to split our list: # Split a Python List into Chunks using numpy import numpy as np our_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] our_array = np.array(our_list) chunk_size = 3 chunked_arrays = np.array_split(our_array, len(our_list) // chunk_size + 1) chunked_list = [list(array) for array in chunked_arrays] print(chunked_list) # Returns: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]
How to Split List in Python - AppDividend
appdividend.com › 15 › how-to-split-list-in-pythonJun 15, 2021 · To split a list in Python, call the len(iterable) method with iterable as a list to find its length and then floor divide the length by 2 using the // operator to find the middle_index of the list. list = [11, 18, 19, 21] length = len(list) middle_index = length // 2 first_half = list[:middle_index] second_half = list[middle_index:] print(first_half) print(second_half)
Custom list split in Python - Tutorialspoint
www.tutorialspoint.com › custom-list-split-in-pythonMay 04, 2020 · from itertools import chain Alist = ['Mon', 'Tue', 'Wed', 6, 7, 'Thu', 'Fri', 11, 21, 4] # The indexes to split at split_points = [2, 5, 8] # Given list print("Given list : ", str(Alist)) # Split at print("The points of splitting : ",split_points) # to perform custom list split sublists = zip(chain([0], split_points), chain(split_points, [None])) split_list = list(Alist[i : j] for i, j in sublists) # printing result print("The split lists are : ", split_list)