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:

  1. eles = True
  2. ele = eles[0]
  3. print(ele)
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

  1. eles = True
  2. if isinstance(eles, bool):
  3. print(eles)
  4. else:
  5. ele = eles[0]
  6. print(ele)
eles = True

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

Then this type error will be fixed.

Leave a Reply