Select Page

How to Solve Python AttributeError: ‘list’ object has no attribute ‘strip’

by | Programming, Python, Tips

In Python, the list data structure stores elements in sequential order. We can use the String strip() method to remove specified characters at the beginning and end of a string. However, we cannot apply the strip() function to a list. If you try to use the strip() method on a list, you will raise the error “AttributeError: ‘list’ object has no attribute ‘strip’”.

This tutorial will go into detail on the error definition. We will go through an example that causes the error and how to solve it.


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

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 ‘strip’” tells us that the list object we are handling does not have the strip attribute. We will raise this error if we try to call the strip() method on a list object. strip() is a string method that removes specified characters from the beginning and end. The default character for the strip() method is whitespace.

Let’s look at an example of calling the strip() method to remove leading white space from a string:

str = "    sheep"

str = str.strip()

print(str)
sheep

Next, let’s look at an example of calling the strip() method to remove leading (at the beginning) and trailing (at the end) characters from a string:

str = "....{{{sheep}}}|...."

str = str.strip(".{}|")

print(str)
sheep

Now we will see what happens if we try to use the strip() method on a list:

a_list = ["   sheep"]

a_list = a_list.strip()

print(a_list)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
      1 a_list = ["   sheep"]
      2 
----≻ 3 a_list = a_list.strip()
      4 
      5 print(a_list)

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

The Python interpreter throws the Attribute error because the list object does not have strip() as an attribute.

Example: Using strip() on a List of Strings

Let’s look at an example list of strings containing the names of different planets in our solar system. Each name has leading or trailing white space and a semi-colon separator. We want to use the strip() and split() methods to get a one-dimensional list of the planet names. Let’s look at the code:

planets = ["Jupiter ;Mars  ;   Saturn", "Venus;    Neptune"]

planets_clean = planets.strip().split(";")

print(planets_clean)

We call both strip() and the split() method on the list in the above code. Let’s run the code to get the result:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
----≻ 1 planets_clean = planets.strip().split(";")

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

We can only call the strip() and split() methods on string objects. If we try to call them on a list, we will raise the AttributeError.

Solution: Use List Comprehension

We can split the task into two smaller tasks to solve this error. The first task splits the strings using the split() method within a list comprehension. The split() method returns a list. If we use the split() method within a list comprehension, we will get a two-dimensional list or list of lists. The second task will flatten the list of lists using a second list comprehension, where we will also call the strip() method on the items in each sublist to remove the white space. Let’s look at the code:

planets = ["Jupiter ;Mars  ;   Saturn", "Venus;    Neptune"]

planets_clean = [item.split(";") for item in planets]

print(planets_clean)

planets_clean = [i.strip() for sublist in planets_clean for i in sublist]

print(planets_clean)

When we print the results, we will see the list of lists after the first list comprehension and then the final one-dimensional list after the second comprehension. Let’s run the code the get the final result:

[['Jupiter ', 'Mars  ', '   Saturn'], ['Venus', '    Neptune']]
['Jupiter', 'Mars', 'Saturn', 'Venus', 'Neptune']

There are other ways to flatten a list of lists that you can learn about in the article: How to Flatten a List of Lists in Python.

If we had a list of strings where each item is a single value, we would not need to use the split method, for example:

planets = ["Jupiter  ", "Mars  ",  " Saturn", "Venus    ",   "Neptune"]

planets_clean = [item.strip() for item in planets]

print(planets_clean)
['Jupiter', 'Mars', 'Saturn', 'Venus', 'Neptune']

Summary

Congratulations on reading to the end of this tutorial! The error “AttributeError: ‘list’ object has no attribute ‘strip’” occurs when you try to use the strip() function to remove leading and trailing characters from a list. The strip() function is suitable for string type objects. If you want to use strip(), ensure that you iterate over the items in the list of strings and call the strip method on each item. You can use list comprehension to access the items in the list.

For further reading on AttributeErrors involving the list object, 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.

Have fun and happy researching!