Examples:
>>> mylist = np.array([1, 2, 3, 4, 5, 6])
split_list(mylist,2) will return a list of two lists of three elements - [[1,2,3][4,5,6]].
>>> np.split(mylist, 2)[array([1, 2, 3]), array([4, 5, 6])]
split_list(mylist,3) will return a list of three lists of two elements.
>>> np.split(mylist, 3)[array([1, 2]), array([3, 4]), array([5, 6])]
Last Updated : 03 Apr, 2019
Example:
Input: Input = [1, 2, 3, 4, 5, 6, 7]
length_to_split = [2, 1, 3, 1]
Output: [
[1, 2],
[3],
[4, 5, 6],
[7]
]
Initial list is: [1, 2, 3, 4, 5, 6, 7] Split length list: [2, 1, 3, 1] List after splitting[[1, 2], [3], [4, 5, 6], [7]]
Initial list is: [1, 2, 3, 4, 5, 6, 7] Split length list: [2, 1, 3, 1] List after splitting[[1, 2], [3], [4, 5, 6], [7]]
Initial list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Split length list: [2, 2, 3, 3] List after splitting[[1, 2], [3, 4], [5, 6, 7], [8, 9, 10]]
September 21, 2021February 22, 2022
One of the ways you can split a list is into n different chunks. Let’s see how we can accomplish this by using a for loop:
# Split a Python List into Chunks using For Loops our_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] chunked_list = list() chunk_size = 3 for i in range(0, len(our_list), chunk_size): chunked_list.append(our_list[i: i + chunk_size]) print(chunked_list) # Returns: [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11] ]
Let’s see how we can write a Python list comprehension to break a list into chunks:
# Split a Python List into Chunks using list comprehensions our_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] chunk_size = 3 chunked_list = [our_list[i: i + chunk_size] for i in range(0, len(our_list), chunk_size) ] print(chunked_list) # Returns: [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11] ]
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] ]
Let’s see how we can do this:
# Split a Python List into Chunks using itertools from itertools import zip_longest our_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] chunk_size = 3 chunked_list = list(zip_longest( * [iter(our_list)] * chunk_size, fillvalue = '')) print(chunked_list) # Returns: [(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, '')] chunked_list = [list(item) for item in list(zip_longest( * [iter(our_list)] * chunk_size, fillvalue = ''))] print(chunked_list) # Returns: [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11] ]
February 25, 2022 Leave a Comment
list_of_numbers = [0, 1, 2, 3, 4, 5]
def getSublists(lst, n):
subListLength = len(lst) // n
list_of_sublists = []
for i in range(0, len(lst), subListLength):
list_of_sublists.append(lst[i: i + subListLength])
return list_of_sublists
print(getSublists(list_of_numbers, 3))
#Output: [
[0, 1],
[2, 3],
[4, 5]
]
A more efficient way to split a list into sublists is with yield().
list_of_numbers = [0, 1, 2, 3, 4, 5]
def getSublists(lst, n):
subListLength = len(lst) // n
for i in range(0, len(lst), subListLength):
yield lst[i: i + subListLength]
print(list(getSublists(list_of_numbers, 3)))
#Output: [
[0, 1],
[2, 3],
[4, 5]
]
list_of_numbers = [0, 1, 2, 3, 4, 5]
def getSublists(lst, n):
subListLength = len(lst) // n
return [lst[i: i + subListLength]
for i in range(0, len(lst), subListLength)
]
print(getSublists(list_of_numbers, 3))
#Output: [
[0, 1],
[2, 3],
[4, 5]
]
Below shows what will happen if you cannot divide the length of the list by the number of sublists equally.
list_of_numbers = [0, 1, 2, 3, 4, 5, 6]
def getSublists(lst, n):
subListLength = len(lst) // n
list_of_sublists = []
for i in range(0, len(lst), subListLength):
list_of_sublists.append(lst[i: i + subListLength])
return list_of_sublists
print(getSublists(list_of_numbers, 3))
#Output: [
[0, 1],
[2, 3],
[4, 5],
[6]
]
list_of_numbers = [0, 1, 2, 3, 4, 5]
def getSublists(lst, n):
subListLength = len(lst) // n
return [lst[i: i + subListLength]
for i in range(0, len(lst), subListLength)
]
print(getSublists(list_of_numbers, 3))
#Output: [
[0, 1],
[2, 3],
[4, 5]
]
Example: You’ve got a list of lists [[1, 2], [3, 4], [5, 6]] and you want to convert it into a list of tuples [(1, 2), (3, 4), (5, 6)]. ,Problem: How to convert a list of lists into a list of tuples?,For example, to create a list of lists of integer values, use [[1, 2], [3, 4]]. Each list element of the outer list is a nested list itself.,Say, you want to convert a list of lists [[1, 2], [3, 4]] into a single list [1, 2, 3, 4]. How to achieve this? There are different options:
For example, to create a list of lists of integer values, use [[1, 2], [3, 4]]
. Each list element of the outer list is a nested list itself.
lst = [ [1, 2], [3, 4] ]
Find examples of all three methods in the following code snippet:
lst = [ [1, 2], [3, 4] ] # Method 1: List Comprehension flat_1 = [x for l in lst for x in l ] # Method 2: Unpacking flat_2 = [ * lst[0], * lst[1]] # Method 3: Extend Method flat_3 = [] for l in lst: flat_3.extend(l) # # Check results: print(flat_1) #[1, 2, 3, 4] print(flat_2) #[1, 2, 3, 4] print(flat_3) #[1, 2, 3, 4]
Solution: You can achieve this by using the beautiful (but, surprisingly, little-known) feature of dictionary comprehension in Python.
persons = [ ['Alice', 25, 'blonde'], ['Bob', 33, 'black'], ['Ann', 18, 'purple'] ] persons_dict = { x[0]: x[1: ] for x in persons } print(persons_dict) # { 'Alice': [25, 'blonde'], # 'Bob': [33, 'black'], # 'Ann': [18, 'purple'] }
Here’s the alternative code:
persons = [ ['Alice', 25, 'blonde'], ['Bob', 33, 'black'], ['Ann', 18, 'purple'] ] persons_dict = {} for x in persons: persons_dict[x[0]] = x[1: ] print(persons_dict) # { 'Alice': [25, 'blonde'], # 'Bob': [33, 'black'], # 'Ann': [18, 'purple'] }
Example: Convert the following list of lists
[ [1, 2, 3], [4, 5, 6] ]