Sequence item 0: expected str instance, tuple found (Python)

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

Sequence item 0: expected str instance, tuple found (Python) #

The Python "TypeError: sequence item 0: expected str instance, tuple found" occurs when we call the join() method with an iterable that contains one or more tuples. To solve the error, use a nested join() to join the elements of each tuple.

typeerror sequence item 0 expected str instance tuple found

Here is an example of how the error occurs.

main.py
my_list = [('a', 'b'), ('c', 'd'), ('e', 'f')] # ⛔️ TypeError: sequence item 0: expected str instance, tuple found result = ''.join(my_list)

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

One way to solve the error is to use a generator expression with a nested join().

main.py
my_list = [('a', 'b'), ('c', 'd'), ('e', 'f')] result = ''.join(''.join(tup) for tup in my_list) print(result) # 👉️ 'abcdef'

We called the join() method with each tuple in the list and then joined the strings.

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

If your tuple might contain non-string values, you'll have to convert each value to a string.

main.py
my_list = [(1, 2), 'c', 'd', 'e', 'f'] result = ''.join(''.join(map(str, tup)) for tup in my_list) print(result) # 👉️ '12cdef'

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

If you want to join the tuples into a string, you can pass each tuple to the str() class.

main.py
my_list = [('a', 'b'), ('c', 'd'), ('e', 'f')] result = ''.join(str(tup) for tup in my_list) print(result) # 👉️ "('a', 'b')('c', 'd')('e', 'f')"

In case you declared a tuple by mistake, tuples are constructed in multiple ways:

  • Using a pair of parentheses () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

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"

Conclusion #

The Python "TypeError: sequence item 0: expected str instance, tuple found" occurs when we call the join() method with an iterable that contains one or more tuples. To solve the error, use a nested join() to join the elements of each tuple.

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.