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

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

If you are looking for a solution to the error AttributeError: ‘list’ object has no attribute ‘find’, here are the causes of the error and some ways you can use it to fix it. Details are below.

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

The error happens because you call the find() method on the list instead of calling the method on the string.

Example

listInfor = ['visit', 'learnshareit', 'website']

# Call the find() method on the list
print(listInfor.find('visit'))

Output:

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

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

Convert the list to a string using the find() method.

Example:

  • If you have a list, you want to use the find() method and then convert that list to a string using the join() function.
listInfor = ['visit', 'learnshareit', 'website']

# Convert the list to a string by the join() function
newStr = ' '.join(listInfor)
print('The string converted to is:', newStr)
print(type(newStr))

# Call the find() method on the list
print('String to look for at the index:', newStr.find('visit'))

Output:

The string converted to is: visit learnshareit website
<class 'str'>
String to look for at the index: 0

Note: The find() method exception will occur if you pass in a string that does not exist. The function will return -1.

Example:

listInfor = ['visit', 'learnshareit', 'website']

# Convert the list to a string by the join() function
newStr = ' '.join(listInfor)
print('The string converted to is:', newStr)
print(type(newStr))

# Call the find() method on the list
print('String to look for at the index:', newStr.find('World Cup'))

Output:

The string converted to is: visit learnshareit website
<class 'str'>
String to look for at the index: -1

As you can see, ‘World Cup’ does not exist in the string, so the function returns -1.

Search for an element in the list.

A convenience of Python is that it has many built-in functions to assist users. Similar to the find() function for searching substrings on strings, the index() function list also has the same function on one’s own.

Syntax:

list.index(element)

The list.index() returns the index of the first occurrence of the search value.

Example:

  • Create a list.
  • Use the index() to return the index of the first occurrence of the search element. 
listInfor = ['visit', 'learnshareit', 'website']

# Use the list.index() indicates the index of the first occurrence of the search element
print('Index of the search element:', listInfor.index('visit'))

Output:

Index of the search element: 0

Note: If the search value does not exist, the program will throw a ValueError error.

Example:

listInfor = ['visit', 'learnshareit', 'website']

# Use the list.index() indicates the index of the first occurrence of the search element
print('Index of the search element:', listInfor.index('Name'))

Output:

Traceback (most recent call last):
 File "./prog.py", line 4, in <module>
ValueError: 'Name' is not in list

‘Name’ does not exist in the list, so when using the index() function, the program throws an error ValueError: ‘Name’ is not in list.

Summary

Through this article, you have an idea to fix the AttributeError: ‘list’ object has no attribute ‘find’ in Python. I suggest you use the list.index() function, 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 *