04-Updating_Editing_Values_in_Dictionaries.py


Sign up Free. Don't forget to check out our challenges, lessons, solve and learn series and more ...


Code Snippet

def main():
    #create a dictionary to store teachers and the subject they teach
    teacher_details={"Name": "Mr Moose","Subject": "Philosophy","Hobby":"Chess","School House":"Red House"}
#update a dictionary by adding a new entry or a key-value pair, modify an existing entry or delete an existing entry


    #UPDATE EXISTING ENTRY
    teacher_details["Name"]="Mr Hanniganahill"
    print(teacher_details)

    print()
    print()
    #ADD NEW ENTRY
    teacher_details["No. of classes"]="12"
    print(teacher_details)
main()
                    

Try it yourself