5 Dictionaries and Structuring Data Flashcards

1
Q

What is a Dictionary?

What is the main difference to lists?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you access a dict´s value?

myCat = {‘size’: ‘fat’, ‘color’: ‘gray’, ‘disposition’: ‘loud’} 2x

A

myCat[‘size’]

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

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

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

spam = {‘color’: ‘red’, ‘age’: 42}

  1. Name 3 methods to return list-like values.
  2. Why are they only list-“like”?
  3. How do return real lists?
A

spam = {‘color’: ‘red’, ‘age’: 42}

  1. spam. keys()
    spam. values()
    spam. items()
  2. Because they cannot be modified in place
  3. list(spam.keys())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you print out a dict´s values one after another?

Why does it work?

A

> > > for i in spam.items():
print(i)

Because the 3x methods return list-“like” values, you can use for loops:

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

How can you assign key and value of a dict to separate variables?

A

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

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

How can you find out the value of a key and return a default value, if that key doesn´t exist in that dictionary?

A
get('key', defaultInteger)
>>> spam = {'color': 'red', 'age': 42}
>>> spam.get('color', 0)
'red'
>>> spam.get('what', 0)
0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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?

A

> > > 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would you count every character in a string with a dictionary?

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

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

What are lists and dictionaries useful for?

A

Lists are useful for ordered values and dictionaries for associating keys with values

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

spam= {‘torch’: 6, ‘arrows’: 12 }

How can you sort a whole dictionary alphabetically
as
1. a List
2. printed out strings

A

spam= {‘arrows’: 1, ‘torch’: 6}

> > > for k, v in sorted(spam.items()):
….

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

How does the output look like?

spam {‘rope’: 1, ‘torch’: 6, ‘gold coin’: 42, ‘dagger’: 1, ‘arrow’: 12}

> > > spam2 = sorted(spam.items())

A

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

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