Select Page

How to Solve Python TypeError: ‘dict’ object is not callable

by | Programming, Python, Tips

A Python dictionary is a collection of data values stored in key-value pairs. To access items in a dictionary, you must use the indexing syntax of square brackets [] with the index position. If you use parentheses, you will raise the “TypeError: ‘dict’ object is not callable”.

This tutorial will describe the error and why it occurs. We will explore an example scenario of this error and go through how to solve it.


TypeError: ‘dict’ object is not callable

Python dictionary is a mutable data structure, meaning we can change the object’s internal state. Dictionaries are iterable objects, which means you can access items individually from inside the dictionary. Accessing an item from a dictionary follows the syntax of using square brackets with the index position. You must specify the appropriate key to access the value you want. If you use an unhashable type to access a dictionary, for example, a slice, you will raise the TypeError: unhashable type: ‘slice’. Let’s look at an example of accessing a dictionary:

pizzas = {

"name1": "margherita",

"name2": "pepperoni",

"name2": "four cheeses"

}
# Access pizza name

print(pizzas["name1"])
margherita

When we run our code, we print the value associated with the key “key1”.

TypeError tells us that we are trying to perform an illegal operation on a Python data object. Specifically, we cannot use parentheses to access dictionary elements. The part “‘dict’ object is not callable” tells us that we are trying to call a dictionary object as if it were a function or method. In Python, functions and methods are callable objects, they have the __call__ method, and you put parentheses after the callable object name to call it. Python dictionary is not a function or method, making calling a dictionary an illegal operation.

Example: Accessing Elements of a Dictionary

Let’s create a program that prints out the values of a dictionary to the console. The dictionary contains information about a type of fundamental particle, the muon.

We will start by declaring a dictionary for the muon data.

# Declare dictionary for muon particle

muon = {

   "name":"Muon",

   "charge":"-1",

   "mass":"105.7",

   "spin":"1/2"

}

The dictionary has four keys and four values. We can use the print() function to print each value to the console.

# Print values for each key in dictionary

print(f'Particle name is: {muon("name")}')

print(f'Particle charge is: {muon("charge")}')

print(f'Particle mass is : {muon("mass")} MeV')

print(f'Particle spin is: {muon("spin")}')

If we run the code, we get the following output:

TypeError                                 Traceback (most recent call last)
1 print(f'Particle name is: {muon("name")}')

TypeError: 'dict' object is not callable

We raise the error because we are not accessing the items with the correct syntax. In the above code, we used parentheses to access items in the dictionary.

Solution

To solve this error, we must replace the parentheses with square brackets to access the items in the muon dictionary.

# Print values for each key in dictionary

print(f'Particle name is: {muon["name"]}')

print(f'Particle charge is: {muon["charge"]}')

print(f'Particle mass is : {muon["mass"]} MeV')

print(f'Particle spin is: {muon["spin"]}')

When we run the code, we will get the following output:

Particle name is: Muon

Particle charge is: -1

Particle mass is : 105.7 MeV

Particle spin is: 1/2

Our code runs successfully and prints four aspects of the muon particle. Instead of using parentheses (), we used square brackets [].

We can also use items() to iterate over the dictionary as follows:

# Iterate over key-value pairs using items()

for key, value in muon.items():

   print(muon[key])

In the above code, we are iterating key-value pairs using items() and printing the value associated with each key. When we run the code, we will get the following output:

Muon

-1

105.7

1/2

Summary

Congratulations on reading to the end of this tutorial. The Python error “TypeError: ‘dict’ object is not callable” occurs when we try to call a dictionary like a function, and the Python dictionary is not a callable object. This error happens when we try to use parentheses instead of square brackets to access items inside a dictionary. You must use square brackets with the key name to access a dictionary item to solve this error.

For further reading on the “not callable” Python TypeError, you can read the following articles:

To learn more about using dictionaries go to the article: Python How to Add to Dictionary.

Go to the Python online courses page to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!