TypeError: ‘int’ object has no attribute ‘__getitem__’

The error TypeError: ‘int’ object has no attribute ‘__getitem__’ is caused by accessing a scalar variable like a collection. In python, the variable is accessed like an array, list, dictionary but it is actually a scalar variable like int, float, long or not containing any value.

In python, the data type of the variable is optional while the variable is declared. When assigning a value for the first time, the data type of the variable is decided. The same variable is created either as a scalar variable such as int, float, string, long etc, or as a collections such as list, array, set, dictionary.

If the scalar variable is used as a collection by accessing any collection functions, the variable can not perform those collection functions. In this case, the python interpeter will throw the error “TypeError: ‘int’ object has no attribute ‘__getitem__'”



Different variations of the error

There are different variations of this type of error due to different data types. The solutions to these errors are the same.

TypeError: 'int' object has no attribute '__getitem__'
TypeError: 'float' object has no attribute '__getitem__'
TypeError: 'long' object has no attribute '__getitem__'
TypeError: 'list' object has no attribute '__getitem__'
TypeError: 'NoneType' object has no attribute '__getitem__'
TypeError: 'int' object is not subscriptable
TypeError: 'float' object is not subscriptable
TypeError: 'NoneType' object is not subscriptable


Exceptions

The error “TypeError: ‘int’ object has no attribute ‘__getitem__’” will be shown as below the stack trace.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print ("the value is " + x[0])
TypeError: 'int' object has no attribute '__getitem__'
[Finished in 0.1s with exit code 1]


Solution 1

If the variable contains scalar values such as int, float, long, boolean, etc., the variable should only be accessed to get values. It’s not supposed to be accessed like an object. Change the access code to read the scalar value. This is going to solve the error.

Program

x = 5
print "the value is " , x[0]

Output

the value is Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print "the value is " , x[0]
TypeError: 'int' object has no attribute '__getitem__'

Solution

x = 5
print "the value is " , x

Output

the value is  5
[Finished in 0.0s]


Solution 2

If the variable is to be a collection object, assign the variable to a collection such as array, list, set, dictionary etc. The variable is to be accessed as an object. This is going to solve the error.

Program

x = 5
print "the value is " , x[0]

Output

the value is Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print "the value is " , x[0]
TypeError: 'int' object has no attribute '__getitem__'

Solution

x = [5, 10]
print "the value is " , x[0]

Output

the value is  5
[Finished in 0.0s]


Solution 3

If the variable value is assigned dynamically and you are not sure of the data type of the variable, first check the data type of the variable using is instance() function and then read the value based on the type. If the variable is a scalar variable, read the value of the scalar variable. If the variable is a collection object, access the collection object member functions.

Program

x = 5
y = [5,10]
print "the value of x[0] is " , x[0]
print "the value of y[0] is " , y[0]

Solution


x = 5
y = [5,10]

if isinstance(x, list):
	print "the value of x[0] is " , x[0]
else :
	print "the value of x is " , x

if isinstance(y, list):
	print "the value of y[0] is " , y[0]
else :
	print "the value of y is " , y

Output

the value of x is  5
the value of y[0] is  5
[Finished in 0.0s]


Solution 4

If the variable value is assigned dynamically and you are not sure of the data type of the variable, first check the data type of the variable using is hasattr() function and then read the value based on the type. If the variable is a scalar variable, read the value of the scalar variable. If the variable is a collection object, access the collection object member functions.

Program

x = 5
y = [5,10]
print "the value of x[0] is " , x[0]
print "the value of y[0] is " , y[0]

Solution

x = 5
y = [5,10]

if hasattr(x, "__len__"):
	print "the value of x[0] is " , x[0]
else :
	print "the value of x is " , x

if hasattr(y, "__len__"):
	print "the value of y[0] is " , y[0]
else :
	print "the value of y is " , y

Output

the value of x is  5
the value of y[0] is  5
[Finished in 0.0s]


Solution 5

If the variable value is assigned dynamically and you are not sure of the data type of the variable, first check the data type of the variable using is type() function and then read the value based on the type. If the variable is a collection object, access the Member Object Collection function. Read the value of the scalar variable if the variable is a scalar variable.

Program

x = 5
y = [5,10]
print "the value of x[0] is " , x[0]
print "the value of y[0] is " , y[0]

Solution

x = 5
y = [5,10]

if type(x) in (tuple, list):
	print "the value of x[0] is " , x[0]
else :
	print "the value of x is " , x

if type(y) in (tuple, list):
	print "the value of y[0] is " , y[0]
else :
	print "the value of y is " , y

Output

the value of x is  5
the value of y[0] is  5
[Finished in 0.0s]


Solution 6

If the variable value is assigned dynamically and you are not sure of the data type of the variable, the numpy module provides buit-in functions such as isscalar() to check the data type of the variable. Read the value based on the type. If the variable is a collection object, access the Member Object Collection function. Read the value of the scalar variable if the variable is a scalar variable.

Solution

import numpy as np

x = 5
y = [5,10]
#print "the value of x[0] is " , x[0]
#print "the value of y[0] is " , y[0]

if np.isscalar(x):
	print "the value of x is " , x
else :
	print "the value of x[0] is " , x[0]

if np.isscalar(y):
	print "the value of y is " , y
else :
	print "the value of y[0] is " , y[0]

Output

the value of x is  5
the value of y[0] is  5
[Finished in 0.0s]


Solution 7

If the variable value is assigned dynamically and you are not sure of the data type of the variable, the numpy module provides buit-in functions such as size() to check the data type of the variable. Read the value based on the type. If the variable is a collection object, access the Member Object Collection function. Read the value of the scalar variable if the variable is a scalar variable.

Solution

import numpy as np

x = 5
y = [5,10]

if np.size(x) == 1:
	print "the value of x is " , x
else :
	print "the value of x[0] is " , x[0]

if np.size(y) == 1:
	print "the value of y is " , y
else :
	print "the value of y[0] is " , y[0]

Output

the value of x is  5
the value of y[0] is  5
[Finished in 0.0s]


Solution 8

If the variable’s data type is not certain and can not check the variable’s data type, then use the try exception else block to fix this error “TypeError: ‘int’ object has no attribute ‘__getitem__'”.

Solution

x = 5
y = [5,10]

try:
    float(x)
except TypeError:
    print "the value of x[0] is " , x[0]
else:
    print "the value of x is " , x

try:
    float(y)
except TypeError:
    print "the value of y[0] is " , y[0]
else:
    print "the value of y is " , y 

Output

the value of x is  5
the value of y[0] is  5
[Finished in 0.0s]



Related Articles

Leave a Reply

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