Dictionaries Flashcards

1
Q

% python
»> D = {‘spam’: 2, ‘ham’: 1, ‘eggs’: 3}
»> D[‘spam’]

> > > D

A
% python
>>> D = {'spam': 2, 'ham': 1, 'eggs': 3} # Make a dictionary
>>> D['spam'] # Fetch a value by key
2
>>> D # Order is scrambled
{'eggs': 3, 'ham': 1, 'spam': 2}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

> > > D
{‘eggs’: 3, ‘ham’: 1, ‘spam’: 2}
D[‘ham’] = [‘grill’, ‘bake’, ‘fry’]
D

> > > del D[‘eggs’]
D

> > > D[‘brunch’] = ‘Bacon’ # Add new entry
D

A

> > > D
{‘eggs’: 3, ‘ham’: 1, ‘spam’: 2}
D[‘ham’] = [‘grill’, ‘bake’, ‘fry’] # Change entry
D
{‘eggs’: 3, ‘ham’: [‘grill’, ‘bake’, ‘fry’], ‘spam’: 2}
del D[‘eggs’] # Delete entry
D
{‘ham’: [‘grill’, ‘bake’, ‘fry’], ‘spam’: 2}
D[‘brunch’] = ‘Bacon’ # Add new entry
D
{‘brunch’: ‘Bacon’, ‘ham’: [‘grill’, ‘bake’, ‘fry’], ‘spam’: 2}

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

> > > D = {‘spam’: 2, ‘ham’: 1, ‘eggs’: 3}
list(D.values())

> > > list(D.items())

> > > D.get(‘spam’) # A key that is there

> > > print(D.get(‘toast’)) # A key that is missing

> > > D.get(‘toast’, 88)

A
>>> D = {'spam': 2, 'ham': 1, 'eggs': 3}
>>> list(D.values())
[3, 1, 2]
>>> list(D.items())
[('eggs', 3), ('ham', 1), ('spam', 2)]
Such lists are useful in loops that need to step
>>> D.get('spam') # A key that is there
2
>>> print(D.get('toast')) # A key that is missing
None
>>> D.get('toast', 88)
88
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

> > > D

> > > D2 = {‘toast’:4, ‘muffin’:5}
D.update(D2)
D

A
>>> D
{'eggs': 3, 'ham': 1, 'spam': 2}
>>> D2 = {'toast':4, 'muffin':5}
>>> D.update(D2)
>>> D
{'toast': 4, 'muffin': 5, 'eggs': 3, 'ham': 1, 'spam': 2}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
# pop a dictionary by key
>>> D

> > > D.pop(‘muffin’)

> > > D.pop(‘toast’)

> > > D

> > > L = [‘aa’, ‘bb’, ‘cc’, ‘dd’]
L.pop()

> > > L

> > > L.pop(1)

> > > L

A
# pop a dictionary by key
>>> D
{'toast': 4, 'muffin': 5, 'eggs': 3, 'ham': 1, 'spam': 2}
>>> D.pop('muffin')
5
>>> D.pop('toast') # Delete and return from a key
4
>>> D
{'eggs': 3, 'ham': 1, 'spam': 2}
# pop a list by position
>>> L = ['aa', 'bb', 'cc', 'dd']
>>> L.pop() # Delete and return from the end
'dd'
>>> L
['aa', 'bb', 'cc']
>>> L.pop(1) # Delete from a specific position
'bb'
>>> L
['aa', 'cc']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

if we have a managers for football teams manchester united, chelsea, arsenal, have a dictionary with team and manager name. pass a team name to this dictionary to return the manager.

then use for loop to print out the team and each of there respective manager.

A
>>> table = {'Python': 'Guido van Rossum',
... 'Perl': 'Larry Wall',
... 'Tcl': 'John Ousterhout' }
>>>
>>> language = 'Python'
>>> creator = table[language]
>>> creator
'Guido van Rossum'
>>> for lang in table: # Same as: for lang in table.keys()
... print(lang, '\t', table[lang])
...
Tcl John Ousterhout
Python Guido van Rossum
Perl Larry Wall
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

> > > mel = {‘name’: ‘Mark’,
… ‘jobs’: [‘trainer’, ‘writer’],
… ‘web’: ‘www.rmi.net/˜lutz’,
… ‘home’: {‘state’: ‘CO’, ‘zip’:80513}}

> > > mel[‘name’]

> > > mel[‘jobs’]

> > > mel[‘jobs’][1]

A

> > > mel = {‘name’: ‘Mark’,
… ‘jobs’: [‘trainer’, ‘writer’],
… ‘web’: ‘www.rmi.net/˜lutz’,
… ‘home’: {‘state’: ‘CO’, ‘zip’:80513}}

>>> mel['name']
'Mark'
>>> mel['jobs']
['trainer', 'writer']
>>> mel['jobs'][1]
'writer'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

> > > list(zip([‘a’, ‘b’, ‘c’], [1, 2, 3]))

> > > D = dict(zip([‘a’, ‘b’, ‘c’], [1, 2, 3]))
D

A

> > > list(zip([‘a’, ‘b’, ‘c’], [1, 2, 3])) # Zip together keys and values
[(‘a’, 1), (‘b’, 2), (‘c’, 3)]
D = dict(zip([‘a’, ‘b’, ‘c’], [1, 2, 3])) # Make a dict from zip result
D
{‘a’: 1, ‘c’: 3, ‘b’: 2}

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

> > > D = {k: v for (k, v) in zip([‘a’, ‘b’, ‘c’], [1, 2, 3])}

|&raquo_space;> D

A

> > > D = {k: v for (k, v) in zip([‘a’, ‘b’, ‘c’], [1, 2, 3])}
D
{‘a’: 1, ‘c’: 3, ‘b’: 2}

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

> > > D = {x: x ** 2 for x in [1, 2, 3, 4]}

|&raquo_space;> D

A

> > > D = {x: x ** 2 for x in [1, 2, 3, 4]} # Or: range(1, 5)
D
{1: 1, 2: 4, 3: 9, 4: 16}

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