Skip to content

How to Resolve Python "TypeError: sequence item 0: expected str instance" with 'join()'

Fix the TypeError: sequence item 0: expected str instance when using str.join() by converting non-string elements (int, float, None, bytes, etc.) to strings with map(), comprehensions, or type-specific handling.

The TypeError: sequence item 0: expected str instance, <type> found is a common error in Python encountered when using the str.join() method. This error signifies that the iterable you're trying to join contains at least one element that is not a string.

This guide explains why this error occurs for various data types (list, float, NoneType, int, tuple, bytes) and provides clear solutions.

Understanding str.join() and the TypeError

The str.join(iterable) method concatenates the elements of an iterable (like a list, tuple, etc.) into a single string. Crucially, all elements within the iterable must be strings. If join() encounters any element that is not a string, it raises a TypeError, indicating the type it found instead.

The specific error message changes based on the type of the first non-string element encountered:

  • TypeError: sequence item 0: expected str instance, list found
  • TypeError: sequence item 0: expected str instance, float found
  • TypeError: sequence item 0: expected str instance, NoneType found
  • TypeError: sequence item 0: expected str instance, int found
  • TypeError: sequence item 0: expected str instance, tuple found
  • TypeError: sequence item 0: expected str instance, bytes found

Despite the different type names mentioned, the root cause and the primary solution remain the same.

General Solution: Converting Items to Strings

To solve this TypeError, you must ensure that every item in the iterable passed to join() is a string before the join() method is called.

Using map()

The map(str, iterable) function applies the str() constructor to each item in the iterable, converting them to strings.

my_list = [1, 2.5, 'a', None, b'byte']

string_list = map(str, my_list)

result = ','.join(string_list)
print(result)  # Output: 1,2.5,a,None,b'byte'

Using Generator Expressions or List Comprehensions

Alternatively, use a generator expression or list comprehension to explicitly convert each item to a string.

my_list = [1, 2.5, 'a', None, b'byte']

result_gen = '_'.join(str(item) for item in my_list)
print(result_gen)       # Output: 1_2.5_a_None_b'byte'

result_list_comp = ''.join([str(item) for item in my_list])
print(result_list_comp) # Output: 12.5aNoneb'byte'
  • Both approaches iterate through my_list, apply str() to each item, and then join() concatenates the resulting strings.

Handling Specific Non-String Types

While the general solution works for most types, some require specific handling depending on the desired output.

list or tuple found

If your iterable contains nested lists or tuples, simply converting them to strings might not be what you want, as it includes brackets/parentheses.

my_list_nested = [['a', 'b'], ('c', 1)]
try:
    result = ''.join(my_list_nested)
    print(result)
except TypeError as e:
    print(e)

result_nested_join = ''.join(''.join(map(str, sublist)) for sublist in my_list_nested)
print(result_nested_join)   # Output: ab c1

result_str_repr = ' | '.join(str(sublist) for sublist in my_list_nested)
print(result_str_repr)      # Output: ['a', 'b'] | ('c', 1)
  • Choose the nested join if you want to flatten the structure; choose string representation if you want to preserve the visual structure.

float or int found

Use the general solution (map(str, ...) or str(item) for item in ...).

my_list_nums = [1.1, 2, 'a', 3.0]
try:
    result = ''.join(my_list_nums)
    print(result)
except TypeError as e:
    print(e)

result_nums = '_'.join(str(item) for item in my_list_nums)
print(result_nums)  # Output: 1.1_2_a_3.0

NoneType found

  • Option A: Convert None to "None" string: Use the general solution.
  • Option B: Filter out None values: Use filter() or a conditional in a comprehension.
my_list_none = [None, 'a', None, 'b']
try:
    result = ''.join(my_list_none)
    print(result)
except TypeError as e:
    print(e)

result_a = ','.join(str(item) for item in my_list_none)
print(f"Solution A: {result_a}") # Output: Solution A: None,a,None,b

result_b = ''.join(item for item in my_list_none if item is not None)
print(f"Solution B: {result_b}") # Output: Solution B: ab

bytes found

You can not directly join bytes objects with a str separator.

  • Option A: Decode bytes to strings first: Convert all bytes objects to str using .decode() before joining with a string separator.
  • Option B: Use a bytes separator: If all items are bytes, use a bytes separator (like b'') for joining.
my_list_bytes = [b'a', b'b', 'c'] # Mixed types
try:
    result = ''.join(my_list_bytes)
    print(result)
except TypeError as e:
    print(f"Error: {e}")


result_a = ''.join(item.decode('utf-8') if isinstance(item, bytes) else item
                    for item in my_list_bytes)
print(f"Solution A: {result_a}")                # Output: Solution A: abc

my_bytes_list = [b'a', b'b', b'c']
result_b = b' '.join(my_bytes_list)
print(f"Solution B: {result_b}")                # Output: Solution B: b'a b c'
print(f"Decoded B: {result_b.decode('utf-8')}") # Output: Decoded B: a b c

Conclusion

The TypeError: sequence item expected str instance when using str.join() invariably means your iterable contains non-string elements.

  • The standard solution is to convert all elements to strings using map(str, ...), a generator expression, or a list comprehension before calling join().
  • Remember to handle specific types like nested lists/tuples, None, and bytes appropriately based on your desired output.
Navigation