TypeError: ‘dict’ object is not callable in Python – Easy Solutions To Fix

TypeError: ‘dict’ object is not callable in Python

When working with the dictionary, we may encounter the TypeError: ‘dict’ object is not callable in Python error several times. To know how to fix this error, read to the end of this article.

Specific cases causing the error “TypeError: ‘dict’ object is not callable” in Python and how to fix it?

The error occurs when you try to access the dictionary variable the same way we call a function. Continue reading to find out how to avoid this error.

Access a dictionary variable as if calling a function

The error will appear when you access the dictionary variable, like calling a function. See the code sample below to understand.

from pprint import pprint

bookmarks = {
    "learn": "learnshareit.com",
    "relax": "youtube.com",
    "food": "doordash.com",
    "shopping": "amazon.com",
    "news": "nationalgeographic.com"
}

pprint(bookmarks())

Output:

TypeError: 'dict' object is not callable

Parentheses in Python are used to call functions, not to access variables. Therefore, to avoid the TypeError: ‘dict’ object is not callable in Python when we run the program, we should distinguish between functions and variables. Just remove the parenthesis in line 11, and we solve the problem.

from pprint import pprint

bookmarks = {
    "learn": "learnshareit.com",
    "relax": "youtube.com",
    "food": "doordash.com",
    "shopping": "amazon.com",
    "news": "nationalgeographic.com"
}

pprint(bookmarks) # Parentheses removed

Output:

{'food': 'doordash.com',
'learn': 'learnshareit.com',
'news': 'nationalgeographic.com',
'relax': 'youtube.com',
'shopping': 'amazon.com'}

Access an element of the dictionary using parentheses

This error can also occurs when you use a key to access the dictionary, like when you call a function that takes one parameter. Like this:

from pprint import pprint

bookmarks = {
    "learn": "learnshareit.com",
    "relax": "youtube.com",
    "food": "doordash.com",
    "shopping": "amazon.com",
    "news": "nationalgeographic.com"
}

pprint(f"The url to learn programming is {bookmarks('learn')}")

Output:

TypeError: 'dict' object is not callable

To fix this error, similar to the above case, just remember when accessing an element in the dictionary, we use square brackets, and when calling a function, we use parentheses. So you just need to change the parentheses to square brackets, and the problem is solved.

from pprint import pprint

bookmarks = {
    "learn": "learnshareit.com",
    "relax": "youtube.com",
    "food": "doordash.com",
    "shopping": "amazon.com",
    "news": "nationalgeographic.com"
}

# Changed parentheses to square brackets
pprint(f"The url to learn programming is {bookmarks['learn']}") 

Output:

'The url to learn programming is learnshareit.com'

Summary

As you have seen when reading through the article, the error “TypeError: ‘dict’ object is not callable” in Python is easy to make but also very easy to avoid. Furthermore, you will never face this error again if you practice and work a lot with Python. Thank you for reading!

Maybe you are interested:

Leave a Reply

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