Split the elements of a list in Python

avatar

Borislav Hadzhiev

Last updated: Jun 26, 2022

banner

Check out my new book

Split the elements of a list in Python #

To split the elements of a list in Python:

  1. Use a list comprehension to iterate over the list.
  2. On each iteration, call the split() method to split each string.
  3. Return the part of each string you want to keep.
main.py
my_list = ['a-1', 'b-2', 'c-3', 'd-4'] # ✅ split each element in list and keep first part result_1 = [item.split('-', 1)[0] for item in my_list] print(result_1) # 👉️ ['a', 'b', 'c', 'd'] # ✅ split each element in list into nested lists result_2 = [item.split('-') for item in my_list] print(result_2) # 👉 [['a', '1'], ['b', '2'], ['c', '3'], ['d', '4']] # ✅ split each element in list and flatten list result_3 = [item.split('-') for item in my_list] result_3_flat = [item for l in result_3 for item in l] print(result_3_flat) # 👉️ ['a', '1', 'b', '2', 'c', '3', 'd', '4'] # ✅ split specific element in list result_4 = my_list[0].split('-') print(result_4) # 👉️ ['a', '1']

The first example shows how to split each element in the list and only keep the first part.

main.py
my_list = ['a-1', 'b-2', 'c-3', 'd-4'] # ✅ split each element in list and keep first part result_1 = [item.split('-', 1)[0] for item in my_list] print(result_1) # 👉️ ['a', 'b', 'c', 'd']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

The str.split() method splits the original string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

We passed 1 for the maxsplit argument to only split the string once and get the first item.

If you need to split the string on each occurrence of the delimiter, remove the second argument in the call to str.split().

main.py
# 👇️ ['a', 'b.c.d'] print('a.b.c.d'.split('.', 1)) # 👈️ with maxsplit set to 1 # 👇️ ['a', 'b', 'c', 'd'] print('a.b.c.d'.split('.')) # 👈️ without maxsplit

The second example splits each element in the list into nested lists.

main.py
my_list = ['a-1', 'b-2', 'c-3', 'd-4'] # ✅ split each element in list into nested lists result_2 = [item.split('-') for item in my_list] print(result_2) # 👉 [['a', '1'], ['b', '2'], ['c', '3'], ['d', '4']]

You can also flatten the list after you split each string.

main.py
my_list = ['a-1', 'b-2', 'c-3', 'd-4'] # ✅ split each element in list and flatten list result_3 = [item.split('-') for item in my_list] result_3_flat = [item for l in result_3 for item in l] print(result_3_flat) # 👉️ ['a', '1', 'b', '2', 'c', '3', 'd', '4']

The result_3 variable is a two-dimensional list, so we had to use another list comprehension to flatten it.

You can achieve the same result by using a for loop.

main.py
my_list = ['a-1', 'b-2', 'c-3', 'd-4'] # ✅ split each element in list and flatten list result_3 = [item.split('-') for item in my_list] print(result_3) # 👉️ [['a', '1'], ['b', '2'], ['c', '3'], ['d', '4']] result_3_flat = [] for l in result_3: for item in l: result_3_flat.append(item) # 👇️ ['a', '1', 'b', '2', 'c', '3', 'd', '4'] print(result_3_flat)

We used a nested for loop to flatten a list of lists.

The outer for loop is used to iterate over the two-dimensional list.

In order to get access to the items in the nested lists, we need a nested for loop.

On each iteration, we append each list item into an empty list.

If you need to split a list item, access the list at the specific index before calling the split() method.

main.py
my_list = ['a-1', 'b-2', 'c-3', 'd-4'] # ✅ split specific element in list result_4 = my_list[0].split('-') print(result_4) # 👉️ ['a', '1'] print(result_4[0]) # 👉️ a print(result_4[1]) # 👉️ 1 # 👇️ ['b', '2'] print(my_list[1].split('-'))

Indexes are zero-based in Python, so the first item in a list has an index of 0, the second an index of 1, etc.

Use the search field on my Home Page to filter through my more than 3,000 articles.