Attributeerror list object has no attribute split error occurs while invoking the split function with list type object in the place of str object. Because this split() function is defined in str python class, not in the list type of object. The best way is to use slicing indexes of the list in case it is a noncharacter array or list. In case it is a list of chars then if you want to use the split function then convert the list to str type object.
We have already understood the root cause for this AttributeError but let’s replicate it with an example.
Since Sample is a list type of object and we are invoking the split() function with it. Hence we are getting this AttributeError.
The generic way to fix any AttributeError is to either change the function or attribute with similar functionality. Or change the object support that attributes.
List object support Index Slicing which is similar to split(). The only difference is that split does indexing on the basis of the separator. But on an overall basis, we can use it as an alternative.
sample=['A','B','C']
sub=sample[0:1]
print(sub)
This method only applies if the element is of str type. Here we will convert the list to a str object and then we use the split() function. Here the list to str conversion is completely dependent on logic.
sample=['A','B','C']
sample=str(sample)
sub=sample.split(",")
print(sub)
Just Completely Identical to the above error, there are multiple similar errors. The attribute can be different from split like data, DataReader, cursor, decoding, etc. Which all have a similar solution. Just to understand the concept in detail read the below article.
Thanks
Data Science Learner Team