Chapter 6: Dictionaries Flashcards
create a simple dictionary and call a value
alien_0 = {‘color’: ‘green’, ‘points’: 5}
print(alien_0[‘color’])
print(alien_0[‘points’])
what are key-value pairs
a set of values associated with each other
each key is connected to a value, and you can use a key to access the value associated with that key
access a value from a dictionary by putting it into a variable and then printing an f-string
new_points = alien_0[‘points’]
print(f”You just earned {new_points} points”)
add new key-value pairs
alien_0['x_position'] = 0 alien_0['y_position'] = 25
start with an empty dictionary and fill it
alien_0 = {}
alien_0['color'] = 'green' alien_0['points'] = 5
modify the values in the dictionary
alien_0[‘color’] = ‘yellow’
print(f” The alien is now {alien_0[‘color’]}”)
Track the position of an alien that moves at different speeds. Store a value representing the alien’s current speed and then use it to determine how far to the right the aliens should move.
#move the alien to the right #determine how far to move the alien based on its current speed if alien_0['speed'] =='slow': x_increment = 1 elif alien_0['speed'] == 'medium': x_increment = 2 else: #this must be a fast alien x_increment = 3
#the new position is the old position plus the increment. alien_0['x_position'] = alien_0['x_position'] + x_increment
print(f”New position: {alien_0[‘x_position’]}”)
use the del statement to completely remove a key-value pair
alien_0 = {‘color’: ‘green’, ‘points’: 5}
del alien_0[‘points’]
make a dictionary of people with similar values, and then writhe a variable used in an f-string that states something about one of the people’s values from the dictionary
favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', }
language = favorite_languages[‘sarah’].title()
print(f”Sarah’s favorite language is {language}”)
what does .get()
sets a default value that will be returned if a requested key doesn’t exist
create a dictionary and use .get() to set a default value if a key doesn’t exist
alien_0 = {‘color’: ‘green’, ‘speed’: ‘slow’}
point_value = alien_0.get(‘points’, ‘No point value assigned.’)
print(point_value)
store a person’s username, first name, and last name in a dictionary
then write a for loop to access it with the use of two f-strings separating what the key and value are
user_0 = { 'username': 'efermi', 'first': 'enrico', 'last': 'fermi', }
for key, value in user_0.items():
print(f”\nKey: {key}”)
print(f”Value: {value}”)
what does .item() do
it returns a list of key-value pairs
use the dictionary that compares similar values to write a for loop with f-strings
favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', }
for name, language in favorite_languages.items():
print(f”\t{name.title()}, I see you love {language}!”)
loop through just the keys in a dictionary
favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', }
for name in favorite_languages.keys():
print(name)
loop through a dictionary’s keys using sorted
favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', }
for name in sorted(favorite_languages.keys()):
print(f”{name.title()}, thank you for taking the poll”)
loop through all the values in a dictionaries
favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', }
print(“the following languages have been mentioned:”)
for language in favorite_languagesvalues():
print(language.title())
what does set() do
when you wrap set() around a list that contains duplicate items, Python identifies the unique items in the list and builds a set from those items
use set() in a for loop using the dictionary with the similar values
favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', }
print(“the following languages have been mentioned:”)
for language in set(favorite_languages.values()):
print(language.title())
build a set in a variable, and what does it look like?
languages = {‘python’, ‘python’, ‘ruby’, ‘c’}
print(languages)
make 3 dictionaries and put them into a list
then use a loop to print each out
alien_0 = {'color': 'green', 'points': 5} alien_1 = {'color': 'yellow', 'points': 10} alien_0 = {'color': 'red', 'points': 15}
aliens = [alien_0, alien_1, alien_0]
for alien in aliens:
print(alien)
use range to create 30 aliens and put them into an empty list
print how many were created using len in an f-string
# Make an empty list for storing aliens. aliens = []
# Make 30 green aliens. for alien_number in range(30): new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'} aliens.append(new_alien)
# Show the first 5 aliens. for alien in aliens[:5]: print(alien) print("...")
#show how many aliens have been created print(f"total number of of aliens {len(aliens)}")
what is nesting
store multiple dictionaries in a list or a list as a value in dictionary
use an if statement inside a for loop to modify the aliens if the color is green
# Make 30 green aliens. for alien_number in range(30): new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'} aliens.append(new_alien)
for alien in aliens[:3]: if alien['color'] == 'green': alien['color'] = 'yellow' alien['speed'] = 'medium' alien['points'] = 10