A Python dictionary contains key-value pairs and uses square brackets to access a value inside the dictionary.
If you use curly brackets instead of square brackets, you will get the TypeError: ‘dict’ object is not callable error.
Dictionaries are iterable objects. In order for an item to be accessed, you are required to use the following indexing syntax:
mydict = {"key1": "value1"}
print(mydict["key1"])
This will return the value at index "key1"
. If someone tries to access the value using curly brackets, i.e., ("key1")
instead of ["key"]
, the above error occurs.
Let’s see it implemented in the code:
# the dictionarymydict = {"key1": "value1"}# accesing with square brackets.print(mydict["key1"])
# the dictionarymydict = {"key1": "value1"}# accesing with curly brackets causes the error.print(mydict("key1"))
The solution is very straightforward. Simply, follow the indexing syntax and you will never face this issue.