Chapter 6: Dictionaries Flashcards

1
Q

create a simple dictionary and call a value

A

alien_0 = {‘color’: ‘green’, ‘points’: 5}
print(alien_0[‘color’])
print(alien_0[‘points’])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what are key-value pairs

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

access a value from a dictionary by putting it into a variable and then printing an f-string

A

new_points = alien_0[‘points’]

print(f”You just earned {new_points} points”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

add new key-value pairs

A
alien_0['x_position'] = 0
alien_0['y_position'] = 25
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

start with an empty dictionary and fill it

A

alien_0 = {}

alien_0['color'] = 'green'
alien_0['points'] = 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

modify the values in the dictionary

A

alien_0[‘color’] = ‘yellow’

print(f” The alien is now {alien_0[‘color’]}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A
#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’]}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

use the del statement to completely remove a key-value pair

A

alien_0 = {‘color’: ‘green’, ‘points’: 5}

del alien_0[‘points’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A
favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
    }

language = favorite_languages[‘sarah’].title()
print(f”Sarah’s favorite language is {language}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what does .get()

A

sets a default value that will be returned if a requested key doesn’t exist

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

create a dictionary and use .get() to set a default value if a key doesn’t exist

A

alien_0 = {‘color’: ‘green’, ‘speed’: ‘slow’}
point_value = alien_0.get(‘points’, ‘No point value assigned.’)
print(point_value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A
user_0 = {
    'username': 'efermi',
    'first': 'enrico',
    'last': 'fermi',
    }

for key, value in user_0.items():
print(f”\nKey: {key}”)
print(f”Value: {value}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what does .item() do

A

it returns a list of key-value pairs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

use the dictionary that compares similar values to write a for loop with f-strings

A
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}!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

loop through just the keys in a dictionary

A
favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
    }

for name in favorite_languages.keys():
print(name)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

loop through a dictionary’s keys using sorted

A
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”)

17
Q

loop through all the values in a dictionaries

A
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())

18
Q

what does set() do

A

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

19
Q

use set() in a for loop using the dictionary with the similar values

A
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())

20
Q

build a set in a variable, and what does it look like?

A

languages = {‘python’, ‘python’, ‘ruby’, ‘c’}

print(languages)

21
Q

make 3 dictionaries and put them into a list

then use a loop to print each out

A
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)

22
Q

use range to create 30 aliens and put them into an empty list

print how many were created using len in an f-string

A
# 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)}")
23
Q

what is nesting

A

store multiple dictionaries in a list or a list as a value in dictionary

24
Q

use an if statement inside a for loop to modify the aliens if the color is green

A
# 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
25
Q

use an if statement inside a for loop to modify the aliens if the color is yellow

A
# 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
     elif alien['color'] == 'yellow':
        alien['color'] = 'red'
        alien['speed'] = 'fast'
        alien['points'] = 15
26
Q

make a pizza dictionary with crust and toppings as key, but put a list as the value for toppings

make a for loop of the toppings

A

Store information about a pizza being ordered.
pizza = {
‘crust’: ‘thick’,
‘toppings’: [‘mushrooms’, ‘extra cheese’],
}

# Summarize the order.
print(f"You ordered a {pizza['crust']}-crust pizza "
    "with the following toppings:")

for topping in pizza[‘toppings’]:
print(“\t” + topping)

27
Q

nest list in the dictionary of the favorite language

loop through the dictionary and use language as the key to hold each value from the dictionary

use another loop to run through each person’s list

decide how you want the output to be formatted

A
favorite_languages = {
    'jen': ['python', 'ruby'],
    'sarah': ['c'],
    'edward': ['ruby', 'go'],
    'phil': ['python', 'haskell'],
    }

for name, languages in favorite_languages.items():
print(f”\n{name.title()}’s favorite languages are:”)
for language in languages:
print(f”\t{Language.title()}”)

28
Q

make a dictionary with two keys and then add dictionaries for the values

the loop through the users dictionary

then print the users name

then access the information and use the keys to generate a full name and location

print a summary of what we know about the user

A
users = {
    'aeinstein': {
        'first': 'albert',
        'last': 'einstein',
        'location': 'princeton',
        },
    'mcurie': {
        'first': 'marie',
        'last': 'curie',
        'location': 'paris',
        },
}

for username, user_info in users.items():
print(f”\nUsername: {username}”)
full_name = f”{user_info[‘first’]} {user_info[‘last’]}”
location = user_info[‘location’]

print(f"\tFull name: {full_name.title()}")
print(f"\tLocation: {location.title()}")