5 Dictionaries and Structuring Data Flashcards
What is a Dictionary?
What is the main difference to lists?
A Dictionary is a mutable collection of many values.
Main differences:
- list indexes uses integer, dict uses keywords
- lists are ordered, dicts not (there is no dict[0]), thus they can´t be sliced
How can you access a dict´s value?
myCat = {‘size’: ‘fat’, ‘color’: ‘gray’, ‘disposition’: ‘loud’} 2x
myCat[‘size’]
How does spam == bacon and eggs == ham evaluate? Why?
> > > spam = [‘cats’, ‘dogs’, ‘moose’]
bacon = [‘dogs’, ‘moose’, ‘cats’]
spam == bacon
> > > eggs = {‘name’: ‘Zophie’, ‘species’: ‘cat’, ‘age’: ‘8’}
ham = {‘species’: ‘cat’, ‘age’: ‘8’, ‘name’: ‘Zophie’}
eggs == ham
Why? Because lists are ordered, dicts not
>>> spam = ['cats', 'dogs', 'moose'] >>> bacon = ['dogs', 'moose', 'cats'] >>> spam == bacon False >>> eggs = {'name': 'Zophie', 'species': 'cat', 'age': '8'} >>> ham = {'species': 'cat', 'age': '8', 'name': 'Zophie'} >>> eggs == ham True
spam = {‘color’: ‘red’, ‘age’: 42}
- Name 3 methods to return list-like values.
- Why are they only list-“like”?
- How do return real lists?
spam = {‘color’: ‘red’, ‘age’: 42}
- spam. keys()
spam. values()
spam. items() - Because they cannot be modified in place
- list(spam.keys())
How can you print out a dict´s values one after another?
Why does it work?
> > > for i in spam.items():
print(i)
Because the 3x methods return list-“like” values, you can use for loops:
How can you assign key and value of a dict to separate variables?
Multiple assignment trick!
»> spam = {‘color’: ‘red’, ‘age’: 42}
»> for k, v in spam.items():
print(‘Key: ‘ + k + ‘, Value: ‘ + v)
How can you find out the value of a key and return a default value, if that key doesn´t exist in that dictionary?
get('key', defaultInteger) >>> spam = {'color': 'red', 'age': 42} >>> spam.get('color', 0) 'red' >>> spam.get('what', 0) 0
How can you use the setdefault method? (Mind the seperating value!)
What happens, if you call it again on the same dict, but with a different value?
What is a major benefit of the default value?
> > > spam.setdefault(‘color’, ‘white’)
Changing the value, e.g.:
»>spam.setdefault(‘color’, ‘black’)
would have no impact on the default value!
The default value ensures that the key + value exist in the dict
How would you count every character in a string with a dictionary?
message = 'It was a bright cold day in April, and the clocks were striking thirteen.' count = {}
for character in message:
➊ count.setdefault(character, 0)
➋ count[character] = count[character] + 1
print(count)
What are lists and dictionaries useful for?
Lists are useful for ordered values and dictionaries for associating keys with values
spam= {‘torch’: 6, ‘arrows’: 12 }
How can you sort a whole dictionary alphabetically
as
1. a List
2. printed out strings
spam= {‘arrows’: 1, ‘torch’: 6}
> > > for k, v in sorted(spam.items()):
….
How does the output look like?
spam {‘rope’: 1, ‘torch’: 6, ‘gold coin’: 42, ‘dagger’: 1, ‘arrow’: 12}
> > > spam2 = sorted(spam.items())
spam {‘rope’: 1, ‘torch’: 6, ‘gold coin’: 42, ‘dagger’: 1, ‘arrow’: 12}
It creates a list of tuples
»>spam2 = [(‘arrow’, 12), (‘dagger’, 1), (‘gold coin’, 42), (‘rope’, 1), (‘torch’, 6)]