. Advertisement .
..3..
. Advertisement .
..4..
In the program below, I’ve got a list of car brands and I’m adding the new car brand to it.
When I run the code and use the list built-in function to put a new automaker to the list, Python throws a TypeError: ‘builtin function or method’ object is not subscriptable.
cars = ['BMW', 'Audi', 'Ferrari', 'Benz']
# append the new car to the list
cars.append["Ford"]
# print the list of new cars
print(cars)
Output:
Traceback (most recent call last):
File "c:\Personal\IJS\Code\main.py", line 4, in <module>
cars.append["Ford"]
TypeError: 'builtin_function_or_method' object is not subscriptable
Where did I do wrong? Please let me know how to fix it. Thanks.
The cause: The append() method is not being used properly, which is why you are seeing this issue. Despite the fact that the append() function is a built-in function, you are indexing it as though it were an array by using square brackets [].
Solution: By recognizing append() as a proper function rather than indexing can fix error. In order to make a proper function, we simply need to insert parenthesis () in place of the square brackets[].
Output: