In Python, you cannot access values inside a filter
object using indexing syntax.
A filter
object is an iterator containing the items in the specified iterable that satisfy the condition of the function passed to the filter()
function.
We can solve the error by converting the filter object to a list object using the built-in list()
method.
For example,
names = ["Ilya", "Georgios", "Ewan", "Meghan"] selected_names = list(filter(lambda x: x[0].lower() in 'aeiou', names)) first = selected_names[0]
This tutorial will detail the error and how to solve it with code examples.
TypeError: ‘filter’ object is not subscriptable
Let’s break up the error message to understand what the error means. TypeError occurs whenever you attempt to use an illegal operation for a specific data type. The part “filter object” tells us the error concerns an illegal operation for the filter object returned by the built-in filter()
method.
The part “is not subscriptable” tells us we cannot access an element of the filter
object using the subscript operator, which is square brackets []
.
A subscriptable object is a container for other objects and implements the __getitem__()
method. Examples of subscriptable objects include strings, lists, tuples, and dictionaries.
We can check if an object implements the __getitem__()
method by listing its attributes with the dir
function. Let’s call the dir
function and pass a filter
object and a str
object to see their attributes.
names = ["Ilya", "Georgios", "Ewan", "Meghan"] selected_names = filter(lambda x: x[0].lower() in 'aeiou', names) print(dir(selected_names))
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
We can see that __getitems__
is not present in the list of attributes for the filter object.
string = "Python" print(dir(string))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
If we want to check if a specific attribute belongs to an object, we can check for membership using the in
operator.
names = ["Ilya", "Georgios", "Ewan", "Meghan"] selected_names = filter(lambda x: x[0].lower() in 'aeiou', names) # Check type of object print(type(selected_names)) # Check membership of attribute print('__getitem__' in dir(selected_names))
<class 'filter'> False
The variable selected_names is an object of the filter class. We can see that __getitem__
is not an attribute of the filter class.
string = "Python" print(type(string)) print('__getitem__' in dir(string))
<class 'str'> True
We can see that __getitem__
is an attribute of the str
class.
Example
Let’s look at an example of trying to access an element in a filter
object using indexing. First, we will create the function to pass to filter()
.
def large_square(number): squared = number ** 2 if squared > 50: return True else: return False
The above function squares a number and returns True
if the squared value is greater than 50
. Otherwise, the function returns False
.
Next, we will use the filter()
function to filter values in a list of integers. The filter
function takes a function and a sequence as arguments and returns an iterator containing the items for which the function returns True
. If we pass None
instead of a function to filter()
then all of the items in the sequence which evaluate to False
are removed.
The syntax of the filter()
function is:
filter(function or None, iterable) -> filter object
numbers = [2, 3, 4, 7, 8, 10, 17] filtered_numbers = filter(large_square, numbers)
Next, we will try to access the first element from the filter
object and print it to the console
first_filtered_number = filtered_numbers[0] print(first_filtered_number)
Let’s run the code to see what happens:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [33], in <cell line: 1>() ----> 1 first_filtered_number = filtered_numbers[0] 3 print(first_filtered_number) TypeError: 'filter' object is not subscriptable
The error occurs because we are trying to access the first element using indexing, which is not possible with filter
objects.
Solution
We can solve this error by converting the filter
object to a list using the built-in list()
method. Let’s look at the revised code:
numbers = [2, 3, 4, 7, 8, 10, 17] filtered_numbers = list(filter(large_square, numbers)) first_filtered_number = filtered_numbers[0] print(first_filtered_number)
Let’s run the code to get the result:
8
The first number in the list that gives a squared value greater than 50
is 8
.
Summary
Congratulations on reading to the end of this tutorial!
For further reading on AttributeErrors, go to the articles:
- How to Solve Python TypeError: ‘zip’ object is not subscriptable
- How to Solve Python TypeError: ‘dict_items’ object is not subscriptable
- How to Solve Python TypeError: ‘generator’ object is not subscriptable
To learn more about Python for data science and machine learning, you can go to the online courses page on Python for the most comprehensive courses.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.