Chapter 5- Dicts Flashcards

1
Q

Dictionary symbol

A

Curly braces

{key:value}

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

How are dictionaries ordered

A

They aren’t
Which means to dictionaries can be equal if they have the same key values in any order
Lists are only equal if they have the same values in the same order
So they can not be sliced

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

Key error

A
Occurs when a value is called for but the keyword does not exist
>>> spam = {'name': 'Zophie', 'age': 7}
>>> spam['color']
Traceback (most recent call last):
  File "", line 1, in 
    spam['color']
KeyError: 'color'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How would you check if a key is in a dictionary

A

➊ birthdays = {‘Alice’: ‘Apr 1’, ‘Bob’: ‘Dec 12’, ‘Carol’: ‘Mar 4’}

➋ if name in birthdays:

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

Keys()

A

> > > spam = {‘color’: ‘red’, ‘age’: 42
for k in spam.keys():
print(k)

color
age

The values returned by these methods are not true lists: They cannot be modified and do not have an append() method.

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

Values()

A

Wil return the values of the dict
»> spam = {‘color’: ‘red’, ‘age’: 42}
»> for v in spam.values():
print(v)

red
42

The values returned by these methods are not true lists: They cannot be modified and do not have an append() method.

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

Items()

A

> > > spam = {‘color’: ‘red’, ‘age’: 42}
For I in spam.items():
print(i)

(‘color’, ‘red’)
(‘age’, 42)

The values returned by these methods are not true lists: They cannot be modified and do not have an append() method.

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

Items() returns the key and value as

A

Tuples (…)
If you want a true list you would have to
list(spam.items())

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

Multiple assignment in for loop for items()

A

> > > spam = {‘color’: ‘red’, ‘age’: 42}
for k, v in spam.items():
print(‘Key: ‘ + k + ‘ Value: ‘ + str(v))

Key: age Value: 42
Key: color Value: red

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

Checking if something is in a dict

A
>>> spam = {'name': 'Zophie', 'age': 7}
>>> 'name' in spam.keys()
True
>>> 'Zophie' in spam.values()
True
>>> 'color' in spam.keys()
False
>>> 'color' not in spam.keys()
True

> > > ‘color’ in spam
False

Short way of checking for keys

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

Get()

A

Takes 2 arguments
KEY if value to return
A fallback if key does not exist

> > > picnicItems = {‘apples’: 5, ‘cups’: 2}
‘I am bringing ‘ + str(picnicItems.get(‘cups’, 0)) + ‘ cups.’
‘I am bringing 2 cups.’
‘I am bringing ‘ + str(picnicItems.get(‘eggs’, 0)) + ‘ eggs.’
‘I am bringing 0 eggs.’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
Setdefault()
Replaces this 
spam = {'name': 'Pooka', 'age': 5}
if 'color' not in spam:
    spam['color'] = 'black'
Which is still useful to remember that setting a value involves dict[Key]=value
A

Arg 1- key to check for
Arg 2-the value to set the key if it doesn’t exist
»> spam = {‘name’: ‘Pooka’, ‘age’: 5}
»> spam.setdefault(‘color’, ‘black’)
‘black’
»> spam
{‘color’: ‘black’, ‘age’: 5, ‘name’: ‘Pooka’}
»> spam.setdefault(‘color’, ‘white’)
‘black’
»> spam
{‘color’: ‘black’, ‘age’: 5, ‘name’: ‘Pooka’}
WIL NOT CHANGE COLOR VALUE 2ND TIME IT IS CALLED CUZ IT ALREADY EXISTS

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

Explain
Message= ‘hello my wonderful wife’

Count=[]
for character in message:
count.setdefault(character, 0)
count[character] = count[character] + 1

print(count)

A

So any time a message is “gone through” it will go character by character. So let’s pick a character

For ‘h’ in message
count.setdefault(h,0)
count[h]= count[h] + 1

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

So character count got a dict of values but prints it rather sloppily and also doesn’t distinguish between A and a
Let’s fix formatting first

A

Import pprint #pretty print

pprint.pprint(count)

{' ': 13,
 ',': 1,
 '.': 1,
 'A': 1,
 'I': 1,
 'a': 4,
Etc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

pprint.pprint(someDictionaryValue)

print(pprint.pformat(someDictionaryValue))

A

Are equal pformat instead of printing to creep converts it to a string

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

theBoard = {‘top-L’: ‘ ‘, ‘top-M’: ‘ ‘, ‘top-R’: ‘ ‘,
‘mid-L’: ‘ ‘, ‘mid-M’: ‘ ‘, ‘mid-R’: ‘ ‘,
‘low-L’: ‘ ‘, ‘low-M’: ‘ ‘, ‘low-R’: ‘ ‘

A

print(board[‘top-L’] + ‘|’ + board[‘top-M’] + ‘|’ + board[‘top-R’])
print(‘-+-+-‘)
print(board[‘mid-L’] + ‘|’ + board[‘mid-M’] + ‘|’ + board[‘mid-R’])
print(‘-+-+-‘)
print(board[‘low-L’] + ‘|’ + board[‘low-M’] + ‘|’ + board[‘low-R’])
printBoard(theBoard)

17
Q

Dictionary components

A

Indexes for dictionaries are called keys, and a key with its associated value is called a key-value pair.

18
Q

Dictionaries inside other dictionaries

A

Look up totalBrpoght() function