TypeError: ‘dict’ object is not callable in Python

Dictionary data structures are used to store multiple collections of unique value in the form of a “Key-Value” pair. In Python, the “dict()” function converts the input value to a dictionary. Sometimes TypeError invokes in Python programs when the user overrides the “dict()” function or calls a dictionary variable as a function.

This Python post will provide different reasons and solutions for “TypeError: dict object is not callable”. The given below aspects are explained in this guide:

Reason 1: Accessing the Dictionary Incorrectly or Calling Dict as a Function

Primarily; this error occurs when the dictionary keys are accessed incorrectly. In Python, calling “dict” as a function can also cause the “dict object is not callable” TypeError in a program. As a reference, the snippet below shows that the parentheses are used to access the dictionary keys:

Upon execution, the error “TypeError: ‘dict’ object is not callable” occurred, as shown in the above image.

Solution 1: Remove Parentheses to Access Dictionary

To solve this error, the parentheses are removed in the following snippet to access the dictionary keys:

Code:

dict_value = {'Name': 'Blake', 'age': 20}
print(dict_value)

In the above code, a variable named “dict_value” containing the key and its value is created. The “print()” function accepts the dictionary variable as an argument and returns the value of the dictionary.

Output:

The dictionary containing the key value is accessed without using the parentheses.

Solution 2: Use Square Bracket to Access Key

If you want to access the “key” value from the dictionary, use a square bracket instead of parentheses. As an example, the following code is provided to show the correct accessing method of dictionary keys:

Code:

dict_value = {'Name': 'Blake', 'age': 20}
print(dict_value['Name'])

In the above code, the dictionary is created, and the value of “Key” named “Name” is accessed from the dictionary.

Output:

From the above output, it is verified that the value of key “Name” is accessed successfully.

Reason 2: Overriding Built-in dict() Function

Another reason which arises from this error is the overriding of the “dict()” function in the Python program. The overriding means the function is defined multiple times in a program with the same name.

The above snippet shows the “TypeError” generated due to the overriding of the “dict()” function.

Solution: Rename the Dictionary Variable

To resolve the “TypeError: ‘dict’ object is not callable” error, make sure to rename the dictionary variable and re-execute the program again:

Code:

dict_val = {'Name': 'Blake', 'age': 20}

print(dict(Name = 'Lily', age = 42))

Now, the dictionary variable is named “dict_value”, thus the overriding is nullified.

Output:

The above output shows that the “dict()” function successfully creates a dictionary. This is because there is no overriding of the built-in “dict()” function in the program.

Conclusion

The “TypeError: dict object is not callable” occurs in Python when a user tries to call the dictionary as a function, overrides the built-in “dict()” function, or incorrectly accesses the dictionary keys. All these errors are related to syntax and understanding of the working of a dictionary in Python. This post has briefly explained the reasons and solutions for the error “TypeError: ‘dict’ object is not callable in Python”.