Sequence item 0: expected str instance, NoneType found

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

banner

Photo by Jonatan Pie

Sequence item 0: expected str instance, NoneType found #

The Python "TypeError: sequence item 0: expected str instance, bool found" occurs when we call the join() method with an iterable that contains one or more None values. To solve the error, figure out where the None values come from or filter them out before calling join.

typeerror sequence item 0 expected str instance nonetype found

Here is an example of how the error occurs.

main.py
my_list = [None, None, 'a', 'b'] # ⛔️ TypeError: sequence item 0: expected str instance, NoneType found result = ''.join(my_list)

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

The best way to solve the error is to figure out where the None values in the iterable came from and correct the assignment.

The most common sources of None values are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if certain condition is met.

If you want to filter out the None values from the iterable, use the filter() function.

main.py
my_list = [None, None, 'a', 'b'] result = ''.join(filter(lambda x: x if x is not None else '', my_list)) print(result) # 👉️ 'ab'

If your iterable contains other values that are not of type string, make sure to pass them to the str() class to convert them to strings before passing them to join().

main.py
my_list = [None, None, 'a', 'b'] result = ''.join(filter(lambda x: str(x) if x is not None else '', my_list)) print(result) # 👉️ 'ab'

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.

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().

main.py
my_list = ['a', 'b', 1, 2] all_strings = list(map(str, my_list)) print(all_strings) # 👉️ ['a', 'b', '1', '2'] result = ''.join(all_strings) print(result) # 👉️ "ab12"

The string the method is called on is used as the separator between elements.

main.py
my_list = ['a', 'b', 'c'] my_str = '-'.join(my_list) print(my_str) # 👉️ "a-b-c"

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.

main.py
my_list = ['a', 'b', 'c'] my_str = ''.join(my_list) print(my_str) # 👉️ "abc"
Use the search field on my Home Page to filter through my more than 1,000 articles.