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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The Python "AttributeError: 'list' object has no attribute 'find'" occurs when we try to call the find() method on a list instead of a string. If you need to check if a value is in a list use in, and if you need to get the index of a value in a list, use the index() method.

attributeerror list object has no attribute find

Here is an example of how the error occurs.

main.py
my_list = ['apple', 'banana', 'kiwi'] # ⛔️ AttributeError: 'list' object has no attribute 'find' print(my_list.find('banana'))

We created a list of 3 elements and tried to call the find() method on it which caused the error because find() is a string method.

If you need to check if a value is in a list, use the in operator.

main.py
my_list = ['apple', 'banana', 'kiwi'] if 'banana' in my_list: print('banana is in the list') # 👉️ this runs else: print('banana is not in the list')

If you need to get the index of a value in a list, use the index() method.

main.py
my_list = ['apple', 'banana', 'kiwi'] result = my_list.index('banana') print(result) # 👉️ 1

The list.index() method returns the index of the first item whose value is equal to the provided argument.

The method raises a ValueError if there is no such item in the list.

You can use a try/except statement to handle the scenario where the item is not in the list.

main.py
my_list = ['apple', 'banana', 'kiwi'] try: result = my_list.index('melon') print(result) except ValueError: print('item is not in the list') # 👉️ this runs

If you meant to call the find() method on a string in the list, you have to access the list at the specific index or iterate over it.

main.py
my_list = ['apple', 'banana', 'kiwi'] result = my_list[0].find('l') print(result) # 👉️ 3

We accessed the list at index 0 and called the find() method to get the index of first occurrence of a substring in the string.

If you only need to check if a substring is found in a string, use the in operator.

main.py
my_list = ['apple', 'banana', 'kiwi'] if 'ppl' in my_list[0]: print('ppl is contained in apple')

The str.find method returns the index of the first occurrence of the provided substring in the string.

The method returns -1 if the substring is not found in the string.

If you need to call a method on each string in the list, use a for loop to iterate over the list.

main.py
my_list = ['apple', 'banana', 'kiwi'] for word in my_list: result = word.find('a') print(result) # 👉️ 0, 1, -1
The "AttributeError: 'list' object has no attribute 'find'" occurs when we try to call the find() method on a list instead of a string.

You can view all the attributes an object has by using the dir() function.

main.py
my_list = ['a', 'b', 'c'] # 👉️ [... 'append', 'clear', 'copy', 'count', 'extend', 'index', # 'insert', 'pop', 'remove', 'reverse', 'sort' ...] print(dir(my_list))

If you pass a class to the dir() function, it returns a list of names of the class's attributes, and recursively of the attributes of its bases.

If you try to access any attribute that is not in this list, you would get the "AttributeError: list object has no attribute" error.

Since find() is not a method implemented by lists, the error is caused.

Conclusion #

The Python "AttributeError: 'list' object has no attribute 'find'" occurs when we try to call the find() method on a list instead of a string. If you need to check if a value is in a list use in, and if you need to get the index of a value in a list, use the index() method.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.