Can we insert values in dictionaries with help of .append()??
Welcome to the forums.
Did you try to use the
.append() method with a dictionary value? What happened?
In short, different data types have specific methods available to them that only work with those data types. Otherwise, you’ll get an error.
You can read here about list methods here:
And updating dictionaries here:
yes i tried using .append() method but its showing key error
the code was:
The output was:
Traceback (most recent call last):
File “script.py”, line 2, in
The error tells us there is no such key as ‘zebras’. The
append() method is a
list attribute, not
If what you are assigning is a value, then use assignment…
animals_in_zoo['zebras'] = 8
That will insert the key and associated value into the dictionary.
ok , got it thank you sir