Chapter 5- Dicts Flashcards Preview

Automate The Boring Stuff python > Chapter 5- Dicts > Flashcards

Flashcards in Chapter 5- Dicts Deck (18):
1

Dictionary symbol

Curly braces
{key:value}

2

How are dictionaries ordered

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

3

Key error

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'

4

How would you check if a key is in a dictionary

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


➋ if name in birthdays:

5

Keys()

>>> 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.

6

Values()

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.

7

Items()

>>> 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.

8

Items() returns the key and value as

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

9

Multiple assignment in for loop for items()

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

10

Checking if something is in a dict

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

11

Get()

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.'

12

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

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

13

Explain
Message= 'hello my wonderful wife'

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

print(count)

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

14

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

Import pprint #pretty print
...
pprint.pprint(count)


{' ': 13,
',': 1,
'.': 1,
'A': 1,
'I': 1,
'a': 4,
Etc

15

pprint.pprint(someDictionaryValue)
print(pprint.pformat(someDictionaryValue))

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

16

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

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

Dictionary components

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

18

Dictionaries inside other dictionaries

Look up totalBrpoght() function