Can't figure out last line:) to add 50 to gold :( nothing works . #14


#1



https://www.codecademy.com/courses/python-beginner-en-pwmb1/2/5?curriculum_id=4f89dab3d788890003000096#


File "python", line 20
inventory['gold'] = inventory['gold']+50
^
SyntaxError: invalid syntax


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

# Adding a key 'burlap bag' and assigning a list to it
inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']

# Sorting the list found under the key 'pouch'
inventory['pouch'].sort() 

# Your code here
inventory['pocket'] = ['seashell', 'strange berry', 'lint']
inventory['backpack'].sort()

#backpack.remove('dagger')
del inventory[backpack.remove[1]
#inventory[backpack.remove('dagger')]
inventory['gold'] = inventory['gold']+50

Continuing the discussion from 14/14 Lists and Dictionaries HELP!:


#2
del inventory[backpack.remove[1]

The above line is causing you problems. First of all you are missing the closing bracket ].

del inventory[backpack.remove[1]]

backpacks.remove is a method so you should instead write:

del inventory[backpack.remove(1)]

But if you want to remove the backpack key from inventory dictionary, you should write:

del inventory["backpack"]

#4

Thanks a lot!
i thought that del inventory["backpack"] removes the whole list, no?


#5

Yes, when you type del inventory["backpack"], you remove the "backpack" key from inventory, which automatically removes its associated value - the whole list.


#6

Then to remove the "dagger" would be del inventory[1] or del inventory['dagger'] ?
I am confused:)
or which one?


#7

Removing the "dagger" element from the list (associated with "backpack" key of inventory dictionary) is different from removing the "backpack" key from inventory dictionary.

Re-read the above line until it makes sense. Then move forward.

I will give you two hints on how to remove "dagger" from that list.

  1. Hint1: You need to access the list first. This is achieved by inventory["backpack"].
  2. Hint2: You need to refer to the docs (fourth heading) and figure out how .remove works :wink:

#8

@methodrockstar58786,
If you have a =dictionary=
like

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

you have an inventory dictionary
which has 3 properties separated from eachother by a comma-,

a gold property with property-key gold and an associated number Value 500
a pouch property with property-key pouch and an associated list Value
a backpack property with property-key backpack and an associated list Value

Now if you use
inventory['backpack']
you are actually accessing the associated list Value
being
['xylophone','dagger', 'bedroll','bread loaf']

Now if you use
inventory['backpack'][0]
you have put your finger on the 1st List-Element which has the index =zero=
['xylophone','dagger', 'bedroll','bread loaf']
being a string Value 'xylophone'

Now if you use
inventory['backpack'][1]
you have put your finger on the 2nd List-Element which has the index =one=
['xylophone','dagger', 'bedroll','bread loaf']
being a string Value 'dagger'

Thus using the del Method
like
del inventory['backpack'][1]
you would =delete= 'dagger' from the =list=
causing the =index-build= of the =list= to be re-calculated as
inventory['backpack']
would now be the =list=
['xylophone', 'bedroll','bread loaf']
and
inventory['backpack'][1]
would now be the string Value 'bedroll'

Reference::
== the Book ==
https://docs.python.org/2/tutorial/datastructures.html
== discussions / opinions ==
http://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists
http://stackoverflow.com/questions/5713218/best-method-to-delete-an-item-from-a-dict

============================================

Imagine you have 2 processes

  • one takes =action-requests= and =append='s those requests to an action-list
    action_list = [ req-1,req-2,req-3]
  • an other will =process= those =action-requests= one at a time, by picking an =action-request= fron the action-list
    You could do it with
    req_to_process = action_list.pop(0) ==> req_to_process would be req-1
    ( this is called FIFO ==> First In First Out )
    or
    req_to_process = action_list.pop() ==> req_to_process would be req-3
    ( this is called LIFO ==> Last In First Out )