How to resolve AttributeError: ‘list’ object has no attribute ‘lower’ in Python

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

While there are times when you will inevitably mistake function calls on data types for which it is not allowed, that is also the cause of the error AttributeError: ‘list’ object has no attribute ‘lower’. The following article will help you find a solution to this error. Hope you will read the whole article.

What causes the AttributeError: ‘list’ object has no attribute ‘lower’?

The error happens because you call lower() on a list instead of you calling it on a string.

The lower() function syntax:

str.lower()

The str.lower() function returns a string that converts all uppercase to lowercase.

Example

uppercaseList = ['VISIT', 'LEARNSHAREIT', 'WEBSITE']

# Call the lower() method on the list
print(uppercaseList.lower())

Output:

Traceback (most recent call last):
 File "./prog.py", line 4, in <module>
AttributeError: 'list' object has no attribute 'lower'

How to solve the AttributeError: ‘list’ object has no attribute ‘lower’?

Convert the list to a string.

If you have a list that you want to use the lower() function on, convert the list to a string first.

Example:

  • Convert the list to a string using the join() function.
uppercaseList = ['VISIT', 'LEARNSHAREIT', 'WEBSITE']

# Use the join() function to convert a list to a string
listConvertToStr = ' '.join(uppercaseList)
print(listConvertToStr)
print(type(listConvertToStr))

# The lower() function converts uppercase letters to lowercase letters
print(listConvertToStr.lower())

Output:

VISIT LEARNSHAREIT WEBSITE
<class 'str'>
visit learnshareit website

Access the list index and then use the lower() function.

Example:

  • You can use the index to access each element in the list and then call lower() on each of those elements.
  • Note: The list index starts from 0 and ends with the length of the list – 1.
uppercaseList = ['VISIT', 'LEARNSHAREIT', 'WEBSITE']

# Access the index and call lower() on the indexes
print(uppercaseList[0].lower())
print(uppercaseList[1].lower())
print(uppercaseList[2].lower())

Output:

visit
learnshareit
website

Use list comprehension or for loop.

Using list comprehension or for loop are also good ideas to fix the error.

Example:

uppercaseList = ['VISIT', 'LEARNSHAREIT', 'WEBSITE']

# Use list comprehension and lower() function
lowerList = [i.lower() for i in uppercaseList]
print(lowerList)

Output:

['visit', 'learnshareit', 'website']

Or use for loop.

Example:

uppercaseList = ['VISIT', 'LEARNSHAREIT', 'WEBSITE']

# Use for loop and lower() function
for i in uppercaseList:
    lowerList = i.lower()
    print(lowerList)

Output:

visit
learnshareit
website

Summary

Through this article, you have an idea to fix the AttributeError: ‘list’ object has no attribute ‘lower’ in Python. Use the list comprehension or for loop so you don’t have to convert the list to a string. Leave a comment so I can know how you feel about the article. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *