Dictionary is a collection of unordered data, which is stored in key-value pairs.
price_list = {
"jeans": 134,
"shirt": 135,
"shoes": 153,
}
A key-value pair is a set of values associated with each other.
price_list = {
"jeans": 134, #key-value pair
"shirt": 135,
"shoes": 153,
}
Key is a unique identifier for some item in the data.
price_list = {
"jeans": #Key
}
The value is that data that is identified or a pointer to the location of the data.
price_list = {
"jeans": 425 #value - 425
}
The update method updates dictionary with the specifies key and value pairs.
price_list.update = ({
"boots": 563,
"sneakers": 326 })
The zip function is used to quickly combine associated data-sets without needing to rely on multi-dimensional lists.
var1 = ["weight", "height"]
var2 = [35, 536]
combined_variables = zip(var1, var2)
The dict function turns the combined list into a dictionary
relative = ["a", "b", "c"]
num = [1, 2, 3]
dictVar = dict(zip(relative, num))
print(dictVar)
test_scores = {"jonathan": 64, "abra": 98, "key": 86, "jane": 36}
print(test_scores["jane"])
KeyError occurs when you input a key that isn’t present in the dictionary/code
check_Key = "andrew"
if check_Key in text_scores:
print(check_Key, "took the test")
else:
print(check_Key, "didn't took the test")
The .get()method shows us the key we’re looking for. If it doesn’t exist, it return none.
print(test_scores.get("abra"))
print(test_scores.get("cong")) #print "None" if it doesn't exist
If you want to delete a key in the dictionary/code
neighbors = {"Ms. Santos": "fruitcake", "Toni": "socks", "Alex": "vinegar"}
neighbors_pop = neighbors.pop("Ms. Santos")
print(neighbors_pop)
If you want to get all the keys without their values
neighbors = {"Ms. Santos": "fruitcake", "Toni": "socks", "Alex": "vinegar"}
y = neighbors.keys()
print(y)
To get all the values without their keys
neighbors = {"Ms. Santos": "fruitcake", "Toni": "socks", "Alex": "vinegar"}
done = neighbors.pop("Ms. Santos")
w = neighbors.values()
print(w)
Shows both keys and values
neighbors = {"Ms. Santos": "fruitcake", "Toni": "socks", "Alex": "vinegar"}
done = neighbors.pop("Ms. Santos")
z = neighbors.items()
print(z)
neighbors = {"Ms. Santos": "fruitcake", "Toni": "socks", "Alex": "vinegar"}
for key, value in neighbors.items():
print(key)
print(value)
for name in neighbors.keys():
print(name)
for name in neighbors.keys():
if name == "Ms. Santos":
continue
print(name)
for gift in neighbors.values():
print(gift)
cat = { 1: {"name": "pepper", "age": 5, "color": "black"},
2: {"name": "sashimi", "age": 6, "color": "gray"} }
print(cat[2]["age"])
print(cat[1]["color"])
cat[3] = {}
cat[3]["name"] = "tuna"
cat[3]["age"] = 2
cat[3]["color"] = "orange"
cat[3]["personality"] = "wild"
print(cat[3])
print(cat[3]["personality"])
print(cat[1])
cat[4] = {"name": "sushi", "age": 10, "color": "white", "personality": "shy"}
print(cat[3])
print(cat[4])
print(cat[4]["age"])