Chapter 5- Dicts Flashcards
Dictionary symbol
Curly braces
{key:value}
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
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'
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:
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.
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.
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.
Items() returns the key and value as
Tuples (…)
If you want a true list you would have to
list(spam.items())
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
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
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.’
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
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
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
pprint.pprint(someDictionaryValue)
print(pprint.pformat(someDictionaryValue))
Are equal pformat instead of printing to creep converts it to a string