Python TypeError: 'dict' object is not callable solution

Posted in /  

Python TypeError: 'dict' object is not callable solution
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    Dictionary is a standard Python data structure that stores the elements in the form of key:value pairs. To access an individual item from the dictionary we put the key name inside a square bracket [] . But if we use the parenthesis () we will receive the "TypeError: 'dict' object is not callable" .

    In this guide, we will discuss the "dict object is not callable" error in detail and learn why Python raises it. We will also walk through a common case scenario where you may encounter this error.

    By the end of this error solution tutorial, you will have a complete idea of why this error raises in a Python program and how to solve it.

    Python Error- TypeError: 'dict' object is not callable

    Python Dictionary is a mutable data structure, and it has a data type of dict . It follows the syntax of the square bracket to access an individual element.

    Example

    students = {"Student1":"Rohan", "Student2":"Rahul", "Student3": "Akash"}
    
    #access student
    print(students["Student1"])    # Rohan

    But if we use parenthesis () instead of the square bracket [] we will receive an error.

    Error Example

    students = {"Student1":"Rohan", "Student2":"Rahul", "Student3": "Akash"}
    
    #access student
    print(students("Student1"))   # TypeError: 'dict' object is not callable

    Error Statement

    This error statement has two parts "TypeError" and " 'dict' object is not callable" TypeError is the Exception Type telling us that we are performing some invalid operation on a Python data object.

    In the above example, we are receiving this exception because we can not use parenthesis to access dictionary elements. 'dict' object is not callable means we are trying to call a dictionary object as a function or method.

    In Python functions and methods are callable objects, we put the parenthesis () after their name when we want to call them. But the dictionary is not a function or method, and when we put the parenthesis after the dictionary name Python throws an error.

    Common Example Scenario

    Now let's discuss an example where you may encounter this error in your Python code. Let's say we have a dictionary human that contains some information about the human species and we need to print all that information on the console panel.

    Example

    #dictionary 
    human = {"family":"Hominidae",
             "class": "Mammalia",
             "species": "Homosapiens",
             "kingdom": "Animalia",
             "average speed": "13km/h",
             "bite force": "70 pounds per square inch"
        }
    
    #print the details
    for key in human:
        print(key, "->", human(key))   #error

    Output

    Traceback (most recent call last):
      File "main.py", line 12, in 
        print(key, "->", human(key))
    TypeError: 'dict' object is not callable

    Break the Error In the above example we are getting the TypeError: 'dict' object is not callable because we have used the () brackets to access the data value from the dictionary human .

    Solution

    To solve the above example, we need to replace the () brackets with [] brackets, while we are accessing the dictionary value using key.

    #dictionary 
    human = {"family":"Hominidae",
             "class": "Mammalia",
             "species": "Homosapiens",
             "kingdom": "Animalia",
             "average speed": "13km/h",
             "bite force": "70 pounds per square inch"
        }
    
    #print the details
    for key in human:
        print(key, "->", human[key]) # solved

    Output

    family -> Hominidae
    class -> Mammalia
    species -> Homosapiens
    kingdom -> Animalia
    average speed -> 13km/h
    bite force -> 70 pounds per square inch

    Now our code runs successfully with no error.

    Conclusion

    The Error "TypeError: 'dict' object is not callable" error raises in a Python program when we use the () brackets to get a dictionary element. To debug this error we need to make sure that we are using the square brackets [] to access the individual element.

    If you are receiving this error in your Python program and could not solve it. You can share your code in the comment section, we will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments