11. New Entries - Using append with dictionaries


#1

hi,

this was my code for this exercise:

menu = {} # Empty dictionary
menu['Chicken Alfredo'] = 14.50 # Adding new key-value pair
print menu['Chicken Alfredo']

menu["Fettucine"] = 13.50
menu["Penne"] = 9.25
menu["Bucatini"] = 22.48

print "There are " + str(len(menu)) + " items on the menu."
print menu

question:

is there a way tidy up the code by using .append or .extend on a dictionary? i was thinking something along the lines of the following:

menu.extend{["Fettucine"] = 13.50, menu["Penne"] = 9.25, menu["Bucatini"] = 22.48}

this and variations of this did not work for me. i'm wondering what i am missing of if it is even possible.

Thank you!


#2

Which exercise exactly?


#3

We do know it is Python. so now we have at least narrowed it down.


#4

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


#5

You could do this:

menu.update({'a':2.50, 'b':5.99, 'c':0.99})

See the update() part of the documentation here: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict


#6

Thank you so much. I'll give it try. Cheers!


#7

That worked perfectly. Thank you so much!


#8