Day 09

Photo by Pisit Heng on Unsplash

Day 09

Dictionaries

Dictionaries are an effective way of storing key-value pairs.

{"Key" : "Value}

#Dictionary which stores the genders of pet dogs
my_pets_dict = {
    "Wifi":"Female",
    "Phantom":"Male",
    "Swami":"Male",
    "KaluBai":"Female",
}
#Retrieving values from a dictionary
my_pets_dict["Wifi"] -> Female

#Adding new pets to the dictionary
my_pets_dict["Jelly"] = "Female"

#Editing values in the dictionary
my_pets_dict["Jelly"] = "Male"

#Clearing the existing dicitonary
my_pets_dict = {}

#Looping through a dictionary returns you the keys
for keys in my_pets_dict:
    print(keys) #keys
    print(my_pets_dict[keys]) #values

Nesting Lists and Dictionaries

#Lists and dictionaries within Dictionaries
#Updating pets dictionary with their favourite food 
my_pets_dict = {
    "Wifi":['Tomato', 'Cheese', 'Dental Stick'],
    "Phantom":['Chicken', 'Poha'],
    "Swami":['Egg'],
    "KaluBai":['Dog Treats', 'Bread'],
}

#Updating pets dictionary with their favourite food and gender
my_pets_dict = {
    "Wifi":{
        "fav_food":['Tomato', 'Cheese', 'Dental Stick'],
        "gender":"Female"
     },
    "Phantom":{
        "fav_food":['Chicken', 'Poha'],
        "gender":"Male"
     },
    "Swami":{
        "fav_food":['Egg'],
        "gender":"Male"
     },
    "KaluBai":{
        "fav_food":['Dog Treats', 'Bread'],
        "gender":"Female"
     },
}

List of Dictionaries

my_pets_list = [
    {
        "name":"Wifi":,
        "fav_food":['Tomato', 'Cheese', 'Dental Stick'],
        "gender":"Female"
    },
    {   "name":"Phantom",
        "fav_food":['Chicken', 'Poha'],
        "gender":"Male"
    },
        "name":"Swami",
        "fav_food":['Egg'],
        "gender":"Male"
    },
        "name":"KaluBai",
        "fav_food":['Dog Treats', 'Bread'],
        "gender":"Female"
    },
]

Project -Secret Auction