5 Best Ways to Split a List of Strings by Space in Python

input_list = ["Hello world", "Python split list example"]
output_list = [s.split() for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

In the code snippet above, we created a list comprehension that iterates over each element in the input list and applies the split() method, effectively breaking the sentences into lists of words based on spaces.

Method 2: Using the re Module

For more complex string splitting needs, Python’s re module can be used to split strings based on regular expressions. This method offers flexibility when the delimiters include different types or patterns of spaces (like tabs or newlines).

Here’s an example:

import re
input_list = ["Hello world", "Python   split list    example"]
output_list = [re.split(r'\s+', s) for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

This code uses the re.split() function with a regex pattern r'\s+' which matches any sequence of whitespace characters to split the strings. This approach is especially useful when the spaces between words aren’t consistent.

Method 3: Using str.split() with a Generator

When dealing with very large lists, you might want to consider a more memory-efficient approach using generators. The split() method can be combined with a generator expression to split strings without creating the entire list in memory at once.

Here’s an example:

input_list = ["Hello world", "Python split list example"]
output_generator = (s.split() for s in input_list)
for item in output_generator:
    print(item)

Output:

["Hello", "world"]
["Python", "split", "list", "example"]

Instead of a list comprehension, we’ve used a generator expression. It will split strings as we iterate through the generator, which is more memory-efficient for large datasets.

Method 4: Using the map() Function

The map() function applies a given function to every element of an iterable and returns a map object (which is an iterator). It can be paired with the split() function to split strings in a list.

Here’s an example:

input_list = ["Hello world", "Python split list example"]
output_list = list(map(str.split, input_list))
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

This snippet maps the split() function to each string in the input list. Note that we convert the map object into a list to view the output in a list format.

Bonus One-Liner Method 5: Using List Comprehension with Filtering

Sometimes you need to not only split strings but also filter out empty strings that could be produced when splitting a string with consecutive spaces. Using list comprehension with a condition can achieve this efficiently in one line.

Here’s an example:

input_list = ["Hello  world", "Python split  list example"]
output_list = [[word for word in s.split(' ') if word] for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

The code uses nested list comprehensions to split each string by spaces and includes an if-statement to filter out any empty strings resulting from consecutive spaces.

Summary/Discussion

  • Method 1: split() Method. Simple and direct approach. Not suitable for complex splitting scenarios where delimiters vary.
  • Method 2: re Module. Highly flexible, especially for patterns of whitespace. Can be overkill for simple tasks and is somewhat slower due to regex processing.
  • Method 3: Generator Expression. Memory efficient, ideal for large datasets. The complexity of generators can be a barrier for those unfamiliar with this concept.
  • Method 4: map() Function. Clean and functional programming approach. Less intuitive for those not accustomed to functional programming paradigms.
  • Method 5: List Comprehension with Filtering. Concise and capable of handling additional filtering tasks within splitting. May be less readable due to nested structures.
import re
input_list = ["Hello world", "Python   split list    example"]
output_list = [re.split(r'\s+', s) for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

This code uses the re.split() function with a regex pattern r'\s+' which matches any sequence of whitespace characters to split the strings. This approach is especially useful when the spaces between words aren’t consistent.

Method 3: Using str.split() with a Generator

When dealing with very large lists, you might want to consider a more memory-efficient approach using generators. The split() method can be combined with a generator expression to split strings without creating the entire list in memory at once.

Here’s an example:

input_list = ["Hello world", "Python split list example"]
output_generator = (s.split() for s in input_list)
for item in output_generator:
    print(item)

Output:

["Hello", "world"]
["Python", "split", "list", "example"]

Instead of a list comprehension, we’ve used a generator expression. It will split strings as we iterate through the generator, which is more memory-efficient for large datasets.

Method 4: Using the map() Function

The map() function applies a given function to every element of an iterable and returns a map object (which is an iterator). It can be paired with the split() function to split strings in a list.

Here’s an example:

input_list = ["Hello world", "Python split list example"]
output_list = list(map(str.split, input_list))
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

This snippet maps the split() function to each string in the input list. Note that we convert the map object into a list to view the output in a list format.

Bonus One-Liner Method 5: Using List Comprehension with Filtering

Sometimes you need to not only split strings but also filter out empty strings that could be produced when splitting a string with consecutive spaces. Using list comprehension with a condition can achieve this efficiently in one line.

Here’s an example:

input_list = ["Hello  world", "Python split  list example"]
output_list = [[word for word in s.split(' ') if word] for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

The code uses nested list comprehensions to split each string by spaces and includes an if-statement to filter out any empty strings resulting from consecutive spaces.

Summary/Discussion

  • Method 1: split() Method. Simple and direct approach. Not suitable for complex splitting scenarios where delimiters vary.
  • Method 2: re Module. Highly flexible, especially for patterns of whitespace. Can be overkill for simple tasks and is somewhat slower due to regex processing.
  • Method 3: Generator Expression. Memory efficient, ideal for large datasets. The complexity of generators can be a barrier for those unfamiliar with this concept.
  • Method 4: map() Function. Clean and functional programming approach. Less intuitive for those not accustomed to functional programming paradigms.
  • Method 5: List Comprehension with Filtering. Concise and capable of handling additional filtering tasks within splitting. May be less readable due to nested structures.
input_list = ["Hello world", "Python split list example"]
output_list = [s.split() for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

In the code snippet above, we created a list comprehension that iterates over each element in the input list and applies the split() method, effectively breaking the sentences into lists of words based on spaces.

Method 2: Using the re Module

For more complex string splitting needs, Python’s re module can be used to split strings based on regular expressions. This method offers flexibility when the delimiters include different types or patterns of spaces (like tabs or newlines).

Here’s an example:

import re
input_list = ["Hello world", "Python   split list    example"]
output_list = [re.split(r'\s+', s) for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

This code uses the re.split() function with a regex pattern r'\s+' which matches any sequence of whitespace characters to split the strings. This approach is especially useful when the spaces between words aren’t consistent.

Method 3: Using str.split() with a Generator

When dealing with very large lists, you might want to consider a more memory-efficient approach using generators. The split() method can be combined with a generator expression to split strings without creating the entire list in memory at once.

Here’s an example:

input_list = ["Hello world", "Python split list example"]
output_generator = (s.split() for s in input_list)
for item in output_generator:
    print(item)

Output:

["Hello", "world"]
["Python", "split", "list", "example"]

Instead of a list comprehension, we’ve used a generator expression. It will split strings as we iterate through the generator, which is more memory-efficient for large datasets.

Method 4: Using the map() Function

The map() function applies a given function to every element of an iterable and returns a map object (which is an iterator). It can be paired with the split() function to split strings in a list.

Here’s an example:

input_list = ["Hello world", "Python split list example"]
output_list = list(map(str.split, input_list))
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

This snippet maps the split() function to each string in the input list. Note that we convert the map object into a list to view the output in a list format.

Bonus One-Liner Method 5: Using List Comprehension with Filtering

Sometimes you need to not only split strings but also filter out empty strings that could be produced when splitting a string with consecutive spaces. Using list comprehension with a condition can achieve this efficiently in one line.

Here’s an example:

input_list = ["Hello  world", "Python split  list example"]
output_list = [[word for word in s.split(' ') if word] for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

The code uses nested list comprehensions to split each string by spaces and includes an if-statement to filter out any empty strings resulting from consecutive spaces.

Summary/Discussion

  • Method 1: split() Method. Simple and direct approach. Not suitable for complex splitting scenarios where delimiters vary.
  • Method 2: re Module. Highly flexible, especially for patterns of whitespace. Can be overkill for simple tasks and is somewhat slower due to regex processing.
  • Method 3: Generator Expression. Memory efficient, ideal for large datasets. The complexity of generators can be a barrier for those unfamiliar with this concept.
  • Method 4: map() Function. Clean and functional programming approach. Less intuitive for those not accustomed to functional programming paradigms.
  • Method 5: List Comprehension with Filtering. Concise and capable of handling additional filtering tasks within splitting. May be less readable due to nested structures.

πŸ’‘ Problem Formulation: Developers are often faced with the need to dissect strings into their constituent parts. For example, you might have a list of sentences and you want to split each sentence into words using spaces as delimiters. The input could be ["Hello world", "Python split list example"], and the desired output would be [["Hello", "world"], ["Python", "split", "list", "example"]]. This article provides several methods to achieve that in Python.

Method 1: Using the split() Method

An easy and straightforward method to split strings by spaces in Python is to use the built-in string.split() method. This method splits a string into a list of words based on the specified separator, which defaults to any whitespace if not provided.

Here’s an example:

input_list = ["Hello  world", "Python split  list example"]
output_list = [[word for word in s.split(' ') if word] for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

The code uses nested list comprehensions to split each string by spaces and includes an if-statement to filter out any empty strings resulting from consecutive spaces.

Summary/Discussion

  • Method 1: split() Method. Simple and direct approach. Not suitable for complex splitting scenarios where delimiters vary.
  • Method 2: re Module. Highly flexible, especially for patterns of whitespace. Can be overkill for simple tasks and is somewhat slower due to regex processing.
  • Method 3: Generator Expression. Memory efficient, ideal for large datasets. The complexity of generators can be a barrier for those unfamiliar with this concept.
  • Method 4: map() Function. Clean and functional programming approach. Less intuitive for those not accustomed to functional programming paradigms.
  • Method 5: List Comprehension with Filtering. Concise and capable of handling additional filtering tasks within splitting. May be less readable due to nested structures.
import re
input_list = ["Hello world", "Python   split list    example"]
output_list = [re.split(r'\s+', s) for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

This code uses the re.split() function with a regex pattern r'\s+' which matches any sequence of whitespace characters to split the strings. This approach is especially useful when the spaces between words aren’t consistent.

Method 3: Using str.split() with a Generator

When dealing with very large lists, you might want to consider a more memory-efficient approach using generators. The split() method can be combined with a generator expression to split strings without creating the entire list in memory at once.

Here’s an example:

input_list = ["Hello world", "Python split list example"]
output_generator = (s.split() for s in input_list)
for item in output_generator:
    print(item)

Output:

["Hello", "world"]
["Python", "split", "list", "example"]

Instead of a list comprehension, we’ve used a generator expression. It will split strings as we iterate through the generator, which is more memory-efficient for large datasets.

Method 4: Using the map() Function

The map() function applies a given function to every element of an iterable and returns a map object (which is an iterator). It can be paired with the split() function to split strings in a list.

Here’s an example:

input_list = ["Hello world", "Python split list example"]
output_list = list(map(str.split, input_list))
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

This snippet maps the split() function to each string in the input list. Note that we convert the map object into a list to view the output in a list format.

Bonus One-Liner Method 5: Using List Comprehension with Filtering

Sometimes you need to not only split strings but also filter out empty strings that could be produced when splitting a string with consecutive spaces. Using list comprehension with a condition can achieve this efficiently in one line.

Here’s an example:

input_list = ["Hello  world", "Python split  list example"]
output_list = [[word for word in s.split(' ') if word] for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

The code uses nested list comprehensions to split each string by spaces and includes an if-statement to filter out any empty strings resulting from consecutive spaces.

Summary/Discussion

  • Method 1: split() Method. Simple and direct approach. Not suitable for complex splitting scenarios where delimiters vary.
  • Method 2: re Module. Highly flexible, especially for patterns of whitespace. Can be overkill for simple tasks and is somewhat slower due to regex processing.
  • Method 3: Generator Expression. Memory efficient, ideal for large datasets. The complexity of generators can be a barrier for those unfamiliar with this concept.
  • Method 4: map() Function. Clean and functional programming approach. Less intuitive for those not accustomed to functional programming paradigms.
  • Method 5: List Comprehension with Filtering. Concise and capable of handling additional filtering tasks within splitting. May be less readable due to nested structures.
input_list = ["Hello world", "Python split list example"]
output_list = [s.split() for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

In the code snippet above, we created a list comprehension that iterates over each element in the input list and applies the split() method, effectively breaking the sentences into lists of words based on spaces.

Method 2: Using the re Module

For more complex string splitting needs, Python’s re module can be used to split strings based on regular expressions. This method offers flexibility when the delimiters include different types or patterns of spaces (like tabs or newlines).

Here’s an example:

import re
input_list = ["Hello world", "Python   split list    example"]
output_list = [re.split(r'\s+', s) for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

This code uses the re.split() function with a regex pattern r'\s+' which matches any sequence of whitespace characters to split the strings. This approach is especially useful when the spaces between words aren’t consistent.

Method 3: Using str.split() with a Generator

When dealing with very large lists, you might want to consider a more memory-efficient approach using generators. The split() method can be combined with a generator expression to split strings without creating the entire list in memory at once.

Here’s an example:

input_list = ["Hello world", "Python split list example"]
output_generator = (s.split() for s in input_list)
for item in output_generator:
    print(item)

Output:

["Hello", "world"]
["Python", "split", "list", "example"]

Instead of a list comprehension, we’ve used a generator expression. It will split strings as we iterate through the generator, which is more memory-efficient for large datasets.

Method 4: Using the map() Function

The map() function applies a given function to every element of an iterable and returns a map object (which is an iterator). It can be paired with the split() function to split strings in a list.

Here’s an example:

input_list = ["Hello world", "Python split list example"]
output_list = list(map(str.split, input_list))
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

This snippet maps the split() function to each string in the input list. Note that we convert the map object into a list to view the output in a list format.

Bonus One-Liner Method 5: Using List Comprehension with Filtering

Sometimes you need to not only split strings but also filter out empty strings that could be produced when splitting a string with consecutive spaces. Using list comprehension with a condition can achieve this efficiently in one line.

Here’s an example:

input_list = ["Hello  world", "Python split  list example"]
output_list = [[word for word in s.split(' ') if word] for s in input_list]
print(output_list)

Output:

[["Hello", "world"], ["Python", "split", "list", "example"]]

The code uses nested list comprehensions to split each string by spaces and includes an if-statement to filter out any empty strings resulting from consecutive spaces.

Summary/Discussion

  • Method 1: split() Method. Simple and direct approach. Not suitable for complex splitting scenarios where delimiters vary.
  • Method 2: re Module. Highly flexible, especially for patterns of whitespace. Can be overkill for simple tasks and is somewhat slower due to regex processing.
  • Method 3: Generator Expression. Memory efficient, ideal for large datasets. The complexity of generators can be a barrier for those unfamiliar with this concept.
  • Method 4: map() Function. Clean and functional programming approach. Less intuitive for those not accustomed to functional programming paradigms.
  • Method 5: List Comprehension with Filtering. Concise and capable of handling additional filtering tasks within splitting. May be less readable due to nested structures.