Skip to content
Jarlin Almanzar edited this page Oct 28, 2019 · 4 revisions

Dictionaries

Def: A collection of key-value pairs. Each key is connect to a value, and you can use a key to access the value associated with that key.
A key can be:

  • a number
  • a string
  • a list
  • another dictionary
  • any object created in python

A dictionary is wrapped in braces {} and a dynamic structure, thus you can add a new key-value pair at any time.

Dictionaries allow you to connect pieces of related information, can store a limitless amount of information, and model a variety of real-world objects.

Dict. alien example

alien_o= {"color": "green", "points":5}
print(alien_o["color"])
print(alien_o["points"])

Adding New Key-Value pairs

alien_o= {"color": "green", "points":5}
print(alien_o["color"])
print(alien_o["points"])
alien_o["x_position"] =0alien_o["y_position"] =25print(alien_o)

Modifying Values in Dictionary

alien_o= {"color": "green", "points":5}
print(alien_o["color"])
print(alien_o["points"])
alien_o["x_position"] =0alien_o["y_position"] =25alien_o["color"] ="blue"print(alien_o)

Removing key-value pairs

del dict["name_of_key"]

alien_o= {"color": "green", "points":5}
print(alien_o["color"])
print(alien_o["points"])
alien_o["x_position"] =0alien_o["y_position"] =25alien_o["color"] ="blue"delalien_o["points"]
print(alien_o)

Using get() to access Values

Why? What if a key-pair value does not exist in a dictionary

alien_o= {"color": "green", "points":5}
print(alien_o["color"])
print(alien_o["points"])
alien_o["x_position"] =0alien_o["y_position"] =25alien_o["color"] ="blue"delalien_o["points"]
point_value=alien_o.get("points", "No point value assigned.")
print(alien_o)
print(point_value)

Looping through a dictionary

user= {
"username" : "The_first",
"first" : "Jarin",
"last" : "Almanzar",
}
forkey, valueinuser.items():
print(f"key is {key}")
print(f"value is {value}\n")

Example 2 Of looping

fav_lang= {
"jarlin" : "python",
"Cris" : "Java",
"Dante" : "SQL",
"Arvin" : "C",
}
forname, languageinfav_lang.items():
print(f"{name}'s favorite language is {language}")

Looping Through all key and values in Dictionary

key() returns the keys of a dict
values() returns the values of a dict values()

fav_lang= {
"jarlin" : "python",
"Cris" : "Java",
"Dante" : "SQL",
"Arvin" : "C",
}
# For keysfornameinfav_lang.keys():
print(name.title())
# For valuesforlanguageinfav_lang.values():
print(language.title())

List of dicts.

alien_0= {"color": "green", "points":5}
alien_1= {"color": "yellow", "points":10}
alien_2= {"color": "red", "points":10}
aliens= [alien_0, alien_1, alien_2]
foralieninaliens:
print(alien)

A List in a Dict

pizza= {
"crust": "thick",
"toppings" : ["mushrooms", "onions", "extra cheese"],
}
print(f"You ordered a {pizza['crust']}-crust pizza")

A dict in a Dict

users= {
"scientist": {
"first" : "albert",
"last" : "einstein",
"location" : "princeton",
},
}
# scientist is the key for alberrt einsteinforusername, user_infoinusers.items():
print(f"\n Username: {username}")
print(f"\n fullname: {user_info['first']} and {user_info['last']}")

Clone this wiki locally