Join multiple strings if they aren't empty in Python


Borislav Hadzhiev

Last updated: Jun 18, 2022


Photo from Unsplash

Join multiple strings if they aren't empty in Python #

To join multiple strings if they aren't empty in Python:

  1. Use the filter() function to filter out all falsy values.
  2. Call the join() method on a string separator.
  3. Pass the filter object to the join() method.
my_strings = ['apple', '', 'banana', '', 'kiwi', ''] result = ' '.join(filter(None, my_strings)) print(result) # 👉️ 'apple banana kiwi'

The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

If you pass None for the function argument, all falsy elements of the iterable are removed.
my_strings = ['apple', '', 'banana', '', 'kiwi', '', None, 0] # 👇️ ['apple', 'banana', 'kiwi'] print(list(filter(None, my_strings)))

Notice that all of the falsy values from the list are filtered out in the result.

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 0 (zero) of any numeric type
  • empty sequences and collections: "" (empty string), () (empty tuple), [] (empty list), {} (empty dictionary), set() (empty set), range(0) (empty range).

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your list contains numbers, or other types, convert all of the values to string before calling join().
my_strings = ['apple', 1, 'banana', 2, 'kiwi', '', None, 0] result = ' '.join(map(str, filter(None, my_strings))) print(result) # 👉️ 'apple 1 banana 2 kiwi'

The string the method is called on is used as the separator between elements.
my_strings = ['apple', '', 'banana', '', 'kiwi', ''] result = '-'.join(filter(None, my_strings)) print(result) # 👉️ 'apple-banana-kiwi'

If you don't need a separator and just want to join the iterable's elements into a string, call the join() method on an empty string.
my_strings = ['a', '', 'b', '', 'c', ''] result = ''.join(filter(None, my_strings)) print(result) # 👉️ 'abc'

An alternative approach to join strings only if they aren't empty is to use a generator expression.
my_strings = ['apple', '', 'banana', '', ' ', 'kiwi', ''] result = ' '.join(item for item in my_strings if item.strip()) print(result) # 👉️ 'apple banana kiwi'

The str.strip method returns a copy of the string with the leading and trailing whitespace removed.

We used it to make sure there aren't any strings that only contain spaces in the result.

We passed a generator expression to the str.join() method.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.