Python

AttributeError: list object has no attribute lower [ Solved ]

AttributeError: list object has no attribute lower error’s root cause is invoking/ accessing lower() attribute from list object although lower() is not defined list class. The lower() attributes belong to str type object/class. This basically converts the string into lowercase. Sometimes we need to convert the elements of a list into lowercase we directly invoke it with list object. Which ultimately throws this AttributeError. , In this article, we will explore the best ways to fix this attributeerror.

list object has no attribute lower

 

AttributeError: list object has no attribute lower ( Solution ) –

Firstly, let’s replicate the issue with some examples and then we will take the same example to explain the fix for this error.

AttributeError list object has no attribute lower root cause

 

Solution 1: Accessing the element as str –

Suppose your intent is to convert a particular element of the list into lowercase str. But Somehow you applied the lower() function in the complete list. Now the interpreter is throwing this list Attributeerror. Now we can fix up this error by accessing individual elements as str objects and then applying the lower function.

sample_list=['A','B','C']
sample_lower=sample_list[0].lower()
print(sample_lower)
list object has no attribute lower fix by accessing element

Solution 2: Accessing full list as str via loop –

Suppose you want to convert the full list of elements into lowercase. Now as we have already observed if we apply the lower() function directly to the list. It will be through the attributeerror. So we will iterate the list via some loop and access each element as str object then convert it into lowercase. Since the idea was to convert the complete list into lowercase hence we will append it back into a different list altogether. Here in the below code, the sample_lower is the final list.

sample_list=['A','B','C']
sample_lower=[]
for i in range(len(sample_list)):
 sample_lower.append(sample_list[i].lower())
print(sample_lower)
list object has no attribute lower fix by accessing complete list

 

Thanks
Data Science Learner Team