Fix TypeError: ‘bool’ object is not subscriptable in Python – Python Tutorial

By | August 14, 2019

In this tutorial, we will introduce how to fix python TypeError: object is not subscriptable. From this tutorial, we can learn why this error occur and how to void and fix it.

Why TypeError: object is not subscriptable occur?

The reason is a python object is not a iteration type, such as list, tuple or dictionary. However, you get element in this object by its position.

For example:

eles = True

ele = eles[0]
print(ele)

You will get this type error.

typeerror - object is not subscriptable

if you set eles = 1, you will get int type error.

TypeError: ‘int’ object is not subscriptable

How to fix this type error?

Check python object instance

eles = True

if isinstance(eles, bool):
    print(eles)
else:
    ele = eles[0]
    print(ele)

Then this type error will be fixed.

Leave a Reply

Your email address will not be published. Required fields are marked *