close

[Solved] Type: ‘type’ object is not subscriptable when indexing in to a dictionary

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Type: ‘type’ object is not subscriptable when indexing in to a dictionary in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Type: ‘type’ object is not subscriptable when indexing in to a dictionary Error Occurs?

Today I get the following error Type: ‘type’ object is not subscriptable when indexing in to a dictionary in python.

How To Solve Type: ‘type’ object is not subscriptable when indexing in to a dictionary Error ?

  1. How To Solve Type: 'type' object is not subscriptable when indexing in to a dictionary Error ?

    To Solve Type: 'type' object is not subscriptable when indexing in to a dictionary Error Because dict is the name of a built-in type in Python you are seeing what appears to be a strange error message, but in reality it is not.

  2. Type: 'type' object is not subscriptable when indexing in to a dictionary

    To Solve Type: 'type' object is not subscriptable when indexing in to a dictionary Error Because dict is the name of a built-in type in Python you are seeing what appears to be a strange error message, but in reality it is not.

Solution 1


Normally Python throws NameError if the variable is not defined:

>>> d[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'd' is not defined

However, you’ve managed to stumble upon a name that already exists in Python.

Because dict is the name of a built-in type in Python you are seeing what appears to be a strange error message, but in reality it is not.

The type of dict is a type. All types are objects in Python. Thus you are actually trying to index into the type object. This is why the error message says that the “‘type’ object is not subscriptable.”

>>> type(dict)
<type 'type'>
>>> dict[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable

Note that you can blindly assign to the dict name, but you really don’t want to do that. It’s just going to cause you problems later.

>>> dict = {1:'a'}
>>> type(dict)
<class 'dict'>
>>> dict[1]
'a'

The true source of the problem is that you must assign variables prior to trying to use them. If you simply reorder the statements of your question, it will almost certainly work:

d = {1: "walk1.png", 2: "walk2.png", 3: "walk3.png"}
m1 = pygame.image.load(d[1])
m2 = pygame.image.load(d[2])
m3 = pygame.image.load(d[3])
playerxy = (375,130)
window.blit(m1, (playerxy))

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read