Dictionaries Flashcards
% python
»> D = {‘spam’: 2, ‘ham’: 1, ‘eggs’: 3}
»> D[‘spam’]
> > > D
% 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}
> > > D
{‘eggs’: 3, ‘ham’: 1, ‘spam’: 2}
D[‘ham’] = [‘grill’, ‘bake’, ‘fry’]
D
> > > del D[‘eggs’]
D
> > > D[‘brunch’] = ‘Bacon’ # Add new entry
D
> > > 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}
> > > 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)
>>> 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
> > > D
> > > D2 = {‘toast’:4, ‘muffin’:5}
D.update(D2)
D
>>> 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}
# 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
# 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']
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.
>>> 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
> > > mel = {‘name’: ‘Mark’,
… ‘jobs’: [‘trainer’, ‘writer’],
… ‘web’: ‘www.rmi.net/˜lutz’,
… ‘home’: {‘state’: ‘CO’, ‘zip’:80513}}
> > > mel[‘name’]
> > > mel[‘jobs’]
> > > mel[‘jobs’][1]
> > > 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'
> > > list(zip([‘a’, ‘b’, ‘c’], [1, 2, 3]))
> > > D = dict(zip([‘a’, ‘b’, ‘c’], [1, 2, 3]))
D
> > > 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}
> > > D = {k: v for (k, v) in zip([‘a’, ‘b’, ‘c’], [1, 2, 3])}
|»_space;> D
> > > D = {k: v for (k, v) in zip([‘a’, ‘b’, ‘c’], [1, 2, 3])}
D
{‘a’: 1, ‘c’: 3, ‘b’: 2}
> > > D = {x: x ** 2 for x in [1, 2, 3, 4]}
|»_space;> D
> > > D = {x: x ** 2 for x in [1, 2, 3, 4]} # Or: range(1, 5)
D
{1: 1, 2: 4, 3: 9, 4: 16}