How to Fix the Python Error: typeerror: 'dict' object is not callable

People use Python for a number of different reasons. But one of Python’s major attractions has always been the flexibility of its data types. Programmers can easily define, manipulate, and even convert variables with ease. But this flexibility can lead to some unexpected situations. For example, the “typeerror: ‘dict’ object is not callable” is often seen when people start to work with Python’s dictionary container. It’s a common, but ultimately easily remedied Python error.

The Core of a Type Error

The “typeerror: ‘dict’ object is not callable” error message states that a line of code is trying to call a Python dictionary data type as a method or function. This is usually just a syntactical error where the programmer has used round brackets to access a dictionary variable instead of the proper square brackets.

A Deeper Dive Into Type Errors and Dictionaries

Understanding this error requires a quick look at Python’s data types. Python has a wide variety of different containers. These are essentially an object that can store other objects. And each container also provides a variety of different techniques to access or sort its contents.

Python’s containers are generally fairly flexible. You can even store one container and anything within it inside of another container of a different type. However, there are some firm rules to keep in mind when using containers. One of the most important is indexing. Most Python containers use a numbered format starting at 0 to access items within its index range. However, that’s not true for Python’s dictionary containers.

Dictionaries are indexed by a key and value pairing. For example, you might write code to access the second item in a variable set to Python’s list. But you wouldn’t do that with dictionaries. You’d instead access items by specifying a key within the dictionary. Sending a key would then return an associated value. This is formatted in a somewhat similar way to how we’d pass values to a method or function when calling it. And it’s often easy to mistakingly format the dictionary reference in the same way we would if it were a method or function.

If the call to a key and value pair is formatted incorrectly then Python’s interpreter will treat it as something quite different. And in this case, we’re seeing an attempt to access the key and value pair as a method or function. The two techniques to interact with the dictionary are worlds apart. But the difference essentially comes down to the type of bracket used to form a reference to the dictionary. Round brackets call something in the manner of a method or function. While square brackets will properly access the dictionary as a collection.

How To Fix the Type Error

Consider the following Python code. It begins by creating a variable called aStates. We then assign a dictionary consisting of US states that begin with the letter ‘a’ and their capitals to the new aStates variable. The code finishes by attempting to print the value associated with the Arizona key.

aStates = {
‘Alabama’ : ‘Montgomery’,
‘Alaska’ : ‘Juneau’,
‘Arizona’: ‘Phoenix’,
‘Arkansas’: ‘Little Rock’,
}
print(aStates(“Arizona”))

This example ultimately fails with a “typeerror: ‘dict’ object is not callable” error. This is due to the use of round rather than square brackets. In the following Python code, we’ll change the type of brackets used to access the aStates variable.

aStates = {
‘Alabama’ : ‘Montgomery’,
‘Alaska’ : ‘Juneau’,
‘Arizona’: ‘Phoenix’,
‘Arkansas’: ‘Little Rock’,
}
print(aStates[“Arizona”])

This code should run cleanly and print Phoenix to the screen. The reason this fix works can be seen in the last line’s formatting. Note that aStates is referenced with square brackets because it’s a collection. However, this reference to the dictionary variable is still contained in round brackets. This is because the reference to the dictionary isn’t a method or function, but the results are being passed to print. And print is itself called as a function.

How to Fix the Python Error: typeerror: 'dict' object is not callable
Scroll to top