In Python, a list is a built-in data type used to store collections of data. We can convert a list of strings to a string using the join() method.

The join is a string method, not a list method.

If we call the join method on a list like list.join(), we will raise the AttributeError: ‘list’ object has no attribute ‘join’.

To solve this error, ensure you use the correct syntax by calling the join() method on the string separator and passing the iterable to join as a parameter.

This tutorial will go through how to solve this error with code examples.


AttributeError: ‘list’ object has no attribute ‘join’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘list’ object has no attribute ‘join’” tells us that the list object does not have the attribute join(). We will raise this error by calling the join() method on a list object. join() is a string method that joins all items in an iterable into one string.

We can think of the correct use as calling the join() method on the separator string with the list we want to join as a parameter. Let’s look at the syntax of the join() method

string.join(iterable)

string is the separator to use when joining the items in the iterable

Parameter:

  • iterable: Required. Any iterable object where all the returned values are strings

Example

Let’s look at an example where we want to join a list of strings that create a URL slug. The code looks as follows:

url_slug_list = ["fun", "article", "about", "dogs"]

url_slug_text = url_slug_list.join("-")

print(f'URL slug is {url_slug_text}')

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-e303ce40f110> in <module>
      1 url_slug_list = ["fun", "article", "about", "dogs"]
----> 2 url_slug_text = url_slug_list.join("-")
      3 print(f'URL slug is {url_slug_text}')

AttributeError: 'list' object has no attribute 'join'

The error occurred because we tried to call the join() method on the list. The join() method is an attribute of the string data type, not the list data type.

Solution

To solve this error, we need to call the join() method on the string separator “-” and then pass the list url_slug_list to the join() call as a parameter. Let’s look at the revised code:

url_slug_list = ["fun", "article", "about", "dogs"]

url_slug_text = "-".join(url_slug_list)

print(f'URL slug is {url_slug_text}')

Let’s run the code to see the result:

URL slug is fun-article-about-dogs

We successfully joined the strings in the list using the “-” separator.

Let’s see what happens when we use white-space as our separator string.

phrase_list =["the", "quick", "brown", "fox", "jumps", "over", "lazy", "dog"]
phrase = ' '.join(phrase_list)
print(f'Phrase with all letters of the alphabet: {phrase}')
Phrase with all letters of the alphabet: the quick brown fox jumps over lazy dog

We can use any string, including white space, as a separator.

Summary

Congratulations on reading to the end of this tutorial! The AttributeError: ‘list’ object has no attribute ‘join’ occurs when we try to call the join() method on a list. The join() method is a string method, not a list method. We have to call the join() method on the separator we want to use to separate the strings in the iterable.

For further reading on AttributeErrors, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.