Python Lists and Dictionaries 14


Good evening to you all Ladies and Gents I have come across what seems to be another problem care to lend a hand? This is the code I wrote:

inventory = {
    'gold' : 500,
    'pouch' : ['flint', 'twine', 'gemstone'],
    'pocket' : ['seashell','strange berry','lint']# 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'

# Your code here

The problem in this situation is that I get an error that the bread loaf which was there from when I started to code and never touched it, I reset the code just to make sure, Is says syntax error and point to in front of the ' and now I am clueless of what to do?


the line above:

'pocket' : ['seashell','strange berry','lint']

missing , at the end of the line, to separate items in a dictionary, in fact you are missing multiply. take a good look

sort and remove can't be inside the dictionary, they manipulate the dictionary, you don't define them inside the dictionary


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

I did this now it's saying pocket is not defined


You're missing the commas after these 2 lists.
Make sure you add your commas otherwise your code won't run and will give you an error.
Hope this works! :slight_smile:
EDIT: Just your your second post, hold on.


that makes sense, pocket is inside your inventory dictionary. So you will need to access the dictionary first:



Check the end of line 4 of your code.


Like this cause if it is I already tried that


I tried this inventory['pocket']
that seems fine but I need to sort it and also now it says that backpack.remove('dagger') is not in the list


I just got the sort function so i'm good on that


Also could you please give me some examples of .sort() being used caused I don't recall using it like inventory['pocket'].sort() just so I get it straight in my mind


what is the problem? inventory['pocket'].sort() is fine, it is just .sort() on a list in a dictionary instead of a variable holding a list, or a list.


Fixed It thank you!:slight_smile: I have soled this problem to the help of you