Python Lists and Dictionaries 14


#1

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']
    pocket.sort()
    backpack.remove('dagger')
}

# 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

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?


#2

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


#3

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']
    
}
pocket.sort()
backpack.remove('dagger')

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


#4

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.


#5

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

dictonaryname['keyname']

#6

Check the end of line 4 of your code.


#7

inventory.sort('pocket')
Like this cause if it is I already tried that


#8

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


#9

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


#10

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


#11

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.


#12

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