Add new entry VALUES to KEY list


#1



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


Traceback (most recent call last):
File "python", line 19, in
AttributeError: 'int' object has no attribute 'insert'


I would like to add a value to key to the list [gold] -> so it would look like: 'gold' : 500, 100

this line > - #inventory['gold'].insert(0,'100') < does not work for me right now. Cannot find the error
p.s. hashtag is not type in my code

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()
inventory['backpack'].remove('dagger')
inventory['gold'] = inventory['gold'] + 50

#inventory['gold'].insert(0,'100')

print inventory


#2

then we go outside the scope of the exercise, then gold key should have a list value. .insert() is a method for list


#3

Yes, it is not in a scope.
However, i tried the insert function as well, but it seems it does not work :slight_smile:


#4

.insert() is a method for list, so then 500 should be of type list not of type integer. You can give the list an initial entry of 500


#5

For initial entry is okey,
but i want to add extra VALUE of 100 to my list. This is what concerns me.


#6

once you update gold value to list, you could use both insert and append to add value to list. Can i see your update where you changed 500 to list?


#7

gold = [500]
gold.insert(1,100)
print gold'

but this is a LIST. Can I insert a value to dictionary?


#8

insert a value to dictionary? You mean to the list belonging to gold key? Yes you can:

inventory['gold'].insert(0,'100')

i would make 100 a integer, not a string. But yes, you should be able to


#9

Yes, u got my point.

Unfortunately I had tried to type your code before, bus it does not work.
it gives an error:

Traceback (most recent call last):
File "python", line 19, in
AttributeError: 'int' object has no attribute 'insert'


#10

in your dictionary:

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

did you update gold value from integer to list? The error message indicates you didn't.

int means integer, which clearly tells me you didn't make the required change, something unclear about my explanation?


#11

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()
inventory['backpack'].remove('dagger')
inventory['gold'] = inventory['gold'][0] + 50
inventory['gold'].insert(1, 100)

print inventory

it still gives me an error. If put a comment on line #inventory['gold'] = inventory['gold'][0] + 50, it seems to be working. But together those lines are not working:
inventory['gold'] = inventory['gold'][0] + 50
inventory['gold'].insert(1, 100)


#12

if you then overwrite your list with a integer, which you do here:

inventory['gold'] = inventory['gold'][0] + 50

it will not work indeed


#13

aaa..., it is a disaster. Cannot catch that's the point.

Please, Could you explain me why it won't work? :slight_smile:


#14

well as you can see when you run this code:

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()
inventory['backpack'].remove('dagger')
print inventory['gold']
inventory['gold'] = inventory['gold'][0] + 50
print inventory['gold']
inventory['gold'].insert(1, 100)

Look at where i inserted print statement, gold was a list, but then you change it back to integer, and as we determined, .insert() won't work for integers


#15

well, as you said, the .insert() function does not work.
What I want to make in console window is:
'gold': 550
'gold': 550, 100

Is that possible or not?:slight_smile:


#16

yea, because you overwrite the list with integer. To get both result gold value should stay a list, and then you need to manipulate the output when printed to make it look pretty but ensure gold value stays a list


#17

Thank you. Really appreciate for your help :))


#18

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.