Items in a Python dictionary must be called using the indexing syntax. This means you must follow a dictionary with square brackets and the key of the item you want to access. If you try to use curly brackets, Python will return a “TypeError: ‘dict’ object is not callable” error.
In this guide, we talk about this error and why it is raised. We walk through an example of this error in action so you can learn how to solve it in your code.
TypeError: ‘dict’ object is not callable
Dictionaries are iterable objects. This means that you can access items individually from inside a dictionary.
To access an item in a dictionary, you need to use indexing syntax. Here’s an example of indexing syntax in Python:
dictionary = {"key1": "value1"} print(dictionary["key1"])
Run our code. “value” is returned. The value associated with the key “key1” is “value1”.
If we try to access an item from our dictionary using curly brackets, we encounter an error. This is because curly brackets are used to denote function calls in Python.
When Python sees a pair or curly brackets, it thinks you are trying to execute a function.
An Example Scenario
Here, we create a program that prints out all the values in a dictionary to the console. This dictionary contains information about a type of bird, the Starling.
Start by declaring a dictionary:
starling = { "name": "Starling", "Scientific_name": "Sturnus Vulgaris", "conservation_status_uk": "Red", "food": "Invertebrates and fruit" }
This dictionary has four keys and four values. Let’s use print() statements to print each value from our dictionary to the console:
print("Name: " + starling("name")) print("Scientific Name: " + starling("scientific_name")) print("UK Conservation Status: " + starling("conservation_status_uk")) print("What They Eat: " + starling("food"))
This code should print out the values of “name”, “scientific_name”, “conservation_status_uk”, and “food” to the console. Run our code and see what happens:
Traceback (most recent call last): File "main.py", line 8, in <module> print("Name: " + starling("name")) TypeError: 'dict' object is not callable
Our code returns an error.
This is because we are incorrectly accessing items from our dictionary. Dictionary items must be accessed using indexing syntax. In our code above, we’ve tried to use curly brackets to access items in our dictionary.
The Solution
To solve this error, we need to make sure we use square brackets to access items in our dictionary. Every time we access an item from the ‘starling” dictionary, we should use this syntax:
starling["key"]
Use this syntax in our main program:
print("Name: " + starling["name"]) print("Scientific Name: " + starling["scientific_name"]) print("UK Conservation Status: " + starling["conservation_status_uk"]) print("What They Eat: " + starling["food"])
Our code now functions successfully:
Name: Starling Scientific Name: Sturnus Vulgaris UK Conservation Status: Red What They Eat: Invertebrates and fruit
Instead of using curly brackets to access items in our dictionary, we have used square brackets. Square brackets indicate to Python that we want to access items from an iterable object. Curly brackets, on the other hand, indicate a function call.
Conclusion
The “TypeError: ‘dict’ object is not callable” error is raised when you try to use curly brackets to access items from inside a dictionary. To solve this error, make sure you use the proper square bracket syntax when you try to access a dictionary item.
Now you have all the knowledge you need to fix this error in your Python code!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.