In Python, the list data structure stores elements in sequential order. We can use the String lower()
method to get a string with all lowercase characters. However, we cannot apply the lower()
function to a list. If you try to use the lower()
method on a list, you will raise the error “AttributeError: ‘list’ object has no attribute ‘lower’”.
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.
Table of contents
AttributeError: ‘list’ object has no attribute ‘lower’
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 ‘lower’” tells us that the list object we are handling does not have the lower attribute. We will raise this error if we try to call the lower() method on a list object. lower()
is a string method that returns a string with all lowercase characters.
Python lower() Syntax
The syntax for the String method lower()
is as follows:
string.lower()
The lower()
method ignores symbols and numbers.
Let’s look at an example of calling the lower()
method on a string:
a_str = "pYTHoN" a_str = a_str.lower() print(a_str)
python
Next, we will see what happens if we try to call lower()
on a list:
a_list = ["pYTHoN", "jAvA", "ruBY", "RuST"] a_list = a_list.lower() print(a_list)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) 1 a_list = ["pYTHoN", "jAvA", "ruBY", "RuST"] 2 ----≻ 3 a_list = a_list.lower() 4 5 print(a_list) AttributeError: 'list' object has no attribute 'lower'
The Python interpreter throws the Attribute error because the list object does not have lower() as an attribute.
Example
Let’s look at an example where we read a text file and attempt to convert each line of text to all lowercase characters. First, we will define the text file, which will contain a list of phrases:
CLiMBinG iS Fun RuNNing is EXCitinG SwimmING iS RElaXiNg
We will save the text file under phrases.txt
. Next, we will read the file and apply lower()
to the data:
data = [line.strip() for line in open("phrases.txt", "r")] print(data) text = [[word for word in data.lower().split()] for word in data] print(text)
The first line creates a list where each item is a line from the phrases.txt
file. The second line uses a list comprehension to convert the strings to lowercase. Let’s run the code to see what happens:
['CLiMBinG iS Fun', 'RuNNing is EXCitinG', 'SwimmING iS RElaXiNg'] --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) 1 data = [line.strip() for line in open("phrases.txt", "r")] 2 print(data) ----≻ 3 text = [[word for word in data.lower().split()] for word in data] 4 print(text) AttributeError: 'list' object has no attribute 'lower'
The error occurs because we applied lower()
to data which is a list. We can only call lower()
on string type objects.
Solution
To solve this error, we can use a nested list comprehension, which is a list comprehension within a list comprehension. Let’s look at the revised code:
data = [line.strip() for line in open("phrases.txt", "r")] print(data) text = [[word.lower() for word in phrase.split()] for phrases in data] print(text)
The nested list comprehension iterates over every phrase, splits it into words using split()
, and calls the lower()
method on each word. The result is a nested list where each item is a list that contains the lowercase words of each phrase. Let’s run the code to see the final result:
['CLiMBinG iS Fun', 'RuNNing is EXCitinG', 'SwimmING iS RElaXiNg'] [['climbing', 'is', 'fun'], ['running', 'is', 'exciting'], ['swimming', 'is', 'relaxing']]
If you want to flatten the list you can use the sum()
function as follows:
flat_text = sum(text, []) print(flat_text)
['climbing', 'is', 'fun', 'running', 'is', 'exciting', 'swimming', 'is', 'relaxing']
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 text file, where each line is a single word we would not need to use a nested list comprehension to get all-lowercase text. The file to use, words.txt contains:
CLiMBinG RuNNing SwimmING
The code to use is as follows:
text = [word.lower() for word in open('words.txt', 'r')] print(text)
Let’s run the code to see the output:
['climbing', 'running', 'swimming']
Summary
Congratulations on reading to the end of this tutorial! The error “AttributeError: ‘list’ object has no attribute ‘lower’” occurs when you try to use the lower()
function to replace a string with another string on a list of strings.
The lower()
function is suitable for string type objects. If you want to use the lower()
method, ensure that you iterate over the items in the list of strings and call the lower method on each item. You can use list comprehension to access the items in the list.
Generally, check the type of object you are using before you call the lower()
method.
For further reading on AttributeErrors involving the list object, go to the articles:
How to Solve Python AttributeError: ‘list’ object has no attribute ‘split’.
How to Solve Python AttributeError: ‘list’ object has no attribute ‘shape’.
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!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.