How to Split Lists in Python
This guide explores various methods for splitting lists in Python to sublists based on several factors, from splitting list items to creating new sublists every N elements or based on some complex conditions.
Splitting List Items with split()
If the elements of your list are strings and you need to split each element into smaller parts based on a delimiter, use a list comprehension with split()
:
my_list = ['a-1', 'b-2', 'c-3', 'd-4']
result = [item.split('-', 1)[0] for item in my_list]
print(result) # Output: ['a', 'b', 'c', 'd']
result2 = [item.split('-', 1)[1] for item in my_list]
print(result2) # Output: ['1', '2', '3', '4']
list_of_ints = [int(x) for x in result2]
print(list_of_ints) # Output: [1, 2, 3, 4]
- Each string in the list is splitted using
"-"
as delimiter. - Then a new list is created and the first element from each of the splitted lists is used to create the new list using list comprehension
[x for x in iterable]
- The second example uses the
1
value for themaxsplit
parameter, causing the split to occur at most once, which returns two items in a list. - If the elements after split are numeric, you can convert them to integers using the
int()
method.
Splitting List Items into Nested Lists
You can create a list of lists where each sublist represents a split item from the original list:
my_list = ['a-1', 'b-2', 'c-3', 'd-4']
result = [item.split('-') for item in my_list]
print(result) # Output: [['a', '1'], ['b', '2'], ['c', '3'], ['d', '4']]
- This example will iterate through all elements from
my_list
, split them using-
as delimiter, and construct a list of the results.
Flattening the Result
To flatten a list of lists into a single list, use a nested list comprehension:
my_list = ['a-1', 'b-2', 'c-3', 'd-4']
result = [item.split('-') for item in my_list] # nested list
result_flat = [item for l in result for item in l] # flatten the list
print(result_flat) # Output: ['a', '1', 'b', '2', 'c', '3', 'd', '4']
- Each element of the list called
result
will be iterated, and then a list comprehension is used to access its elements. The result is a new, flattened list calledresult_flat
.
You can also use the for
loop instead of the nested list comprehension:
my_list = ['a-1', 'b-2', 'c-3', 'd-4']
result = [item.split('-') for item in my_list]
result_flat = []
for l in result:
for item in l:
result_flat.append(item)
print(result_flat)
Splitting a List into Sublists of N Items
You can split a list into sublists of a fixed size N by using list slicing in a list comprehension.
Using List Comprehension
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
n = 2 # Sublist size
result = [my_list[i:i + n] for i in range(0, len(my_list), n)]
print(result) # Output: [['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j']]
- The slice
my_list[i:i + n]
generates a sublist from the indexi
up to (but not including)i+n
. - Then, this slice is created by using a list comprehension and generating the index using
range()
. - This generates nested lists containing only
n
elements.
Using a for
Loop
Alternatively, you can use a for
loop with range()
and a step:
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
n = 2
result = []
for idx in range(0, len(my_list), n):
result.append(my_list[idx:idx+n])
print(result) # Output: [['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j']]
- Same as with list comprehension we use slicing of the original list into sublists of size
n
.
Splitting a List Based on a Condition
To split a list into two lists based on a condition, use two list comprehensions:
my_list = [1, 21, 3, 7, 14, 19, 28, 35]
lte_10 = [item for item in my_list if item <= 10]
gte_10 = [item for item in my_list if item >= 10]
print(lte_10) # Output: [1, 3, 7]
print(gte_10) # Output: [21, 14, 19, 28, 35]
- The first list comprehension
[item for item in my_list if item <= 10]
creates a list calledlte_10
containing only thoseitem
s from the list calledmy_list
that are less than or equal to 10. - The second list comprehension
[item for item in my_list if item >= 10]
creates a list calledgte_10
containing only thoseitem
s from the list calledmy_list
that are greater than or equal to 10.
Splitting Lists using NumPy
You can also use numpy.array_split()
which can be particularly useful for splitting NumPy arrays into a specified number of sub-arrays of equal or near-equal size:
import numpy as np
a_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
arr = np.array_split(a_list, 4) # Split list into 4 arrays
print(arr)
print()
for item in arr:
print(item)
Output:
[array(['a', 'b', 'c'], dtype='<U1'), array(['d', 'e', 'f'], dtype='<U1'), array(['g', 'h'], dtype='<U1'), array(['i', 'j'], dtype='<U1')]
['a' 'b' 'c']
['d' 'e' 'f']
['g' 'h']
['i' 'j']