Skip to content

Instantly share code, notes, and snippets.

@peta909
Last active February 15, 2019 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peta909/b58a671896b3fa31b02d0f70eb313701 to your computer and use it in GitHub Desktop.
Save peta909/b58a671896b3fa31b02d0f70eb313701 to your computer and use it in GitHub Desktop.
Using Python Dict and accessing key and value using indexes
food_cook_dict = {
"pasta" : "Aaron",
"bread" : "Thomas",
"rice" : "John",
"Soup" : "Kate",
"noodles" : "Liam",
}
# dictionary are unordered unlike lists till python 3.6
print ("The following is the menu of the day and their cooks")
for i, x in enumerate(food_cook_dict):
print (str(i) + ". " + str(x))
#no idea how to start the item from index 1 instead of 0.
choice = raw_input("Please Enter selection number: ")#choice is of type(str)
print ("Food choice: " + food_cook_dict.keys()[int(choice)])#access to the key based on index(choice)
print ("Cook assigned: " + food_cook_dict.values()[int(choice)])#access to value based on index(choice)
@peta909
Copy link
Author

peta909 commented Feb 15, 2019

if key is used to select value instead of index:

cook_assigned = food_cook_dict.get(choice, "Invalid Entry")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment