Adding keys to a list


#1

Hallo dear codacademy community

I am currently working my way through the basic Python coding tutorial when I was wondering about something.
(Please refer to Learn Python 2, Python Lists and Dictionaries, Exercise 14: It’s Dangerous to Go Alone! Take This)

To add a string to a list you go like this (EXAMPLE only):

inventory = {
‘gold’ : 500,
‘pouch’ : [‘flint’, ‘twine’, ‘gemstone’], # Assigned a new list to ‘pouch’ key
‘backpack’ : [‘xylophone’,‘dagger’, ‘bedroll’,‘bread loaf’]
}

inventory[‘backpack’].append(“shovel”) # This will add the string “tree” to the key ‘pocket’ in the list inventory.

Why can’t I add an integer the same way? Just like:

inventory[‘gold’].append(50)

Thanks for your help!
416e6469


#2

because methods like .append() are data type specific. You increase/add gold with math operation


#3

Thank you :slight_smile:


#4

Once you learn about classes and methods (functions belonging to class(es)), this should make more sense, but i won’t explain it, given it might just be confusing at this point, the course will cover it later