Env: Python [conda env:PY27_Test]
Env: Python [conda env:PY36]

Working With Dictionaries - The Basics

These excercises come from multiple sources and show the basics of creating, modifying, and sorting dictionaries. All code was created in Python 2.7 and cross-tested in Python 3.6.

Quick Guide to the basics:

  • Create Dictionary: dictionary = { key1:value, key2:value2 }
    • Example: myDict1 = { 'one':'first thing', 'two':'secondthing' }
    • Example: myDict2 = { 1:43, 2:600, 3:-1000.4 }
    • Example: myDict3 = { 1:"text", 2:345, 3:'another value' }
  • Add to a dictionary: myDict1['newVal'] = 'something stupid'
    • Resulting Dictionary: myDict1 = { 'one':'first thing', 'two':'secondthing', 'newVal':'something stupid' }
  • Remove from dictionary: del myDict1['newVal']
    • now myDict1 is back the way it was: { 'one':'first thing', 'two':'secondthing' }

In This Document:


In [35]:
# Ex39 in Learn Python the Hard Way:
#  https://learnpythonthehardway.org/book/ex39.html
# edited, expanded, and made PY3.x compliant by Mitch before inclusion in this notebook

# create a mapping of state to abbreviation
states = {
    'Oregon': 'OR',
    'Florida': 'FL',
    'California': 'CA',
    'New York': 'NY',
    'Michigan': 'MI'
}

# create a basic set of states and some cities in them
cities = {
    'CA': 'San Francisco',
    'MI': 'Detroit',
    'FL': 'Jacksonville'
}

# add some more cities
cities['NY'] = 'New York'
cities['OR'] = 'Portland'

# print out some cities
print('-' * 10)
print("Two cities:")
print("NY State has: %s" %cities['NY'])
print("OR State has: %s" %cities['OR'])

# print some states
print('-' * 10)
print("Abbreviations for Two States:")
# PY 2.7 syntax from original code: print "Michigan's abbreviation is: ", states['Michigan']
print("Michigan's abbreviation is: %s" %states['Michigan'])
print("Florida's abbreviation is: %s" %states['Florida'])

# do it by using the state then cities dict
print('-' * 10)
print("State Abbreviation extracted from cities dictionary:")
print("Michigan has: %s" %cities[states['Michigan']])
print("Florida has: %s" %cities[states['Florida']])

# print every state abbreviation
print('-' * 10)
print("Every State Abbreviation:")
for state, abbrev in states.items():
    print("%s is abbreviated %s" % (state, abbrev))

# print every city in state
print('-' * 10)
print("Every city in Every State:")
for abbrev, city in cities.items():
    print("%s has the city %s" %(abbrev, city))

# now do both at the same time
print('-' * 10)
print("Do Both at Once:")
for state, abbrev in states.items():
    print("%s state is abbreviated %s and has city %s" % (
        state, abbrev, cities[abbrev]))

print('-' * 10)


----------
Two cities:
NY State has: New York
OR State has: Portland
----------
Abbreviations for Two States:
Michigan's abbreviation is: MI
Florida's abbreviation is: FL
----------
State Abbreviation extracted from cities dictionary:
Michigan has: Detroit
Florida has: Jacksonville
----------
Every State Abbreviation:
California is abbreviated CA
Michigan is abbreviated MI
New York is abbreviated NY
Florida is abbreviated FL
Oregon is abbreviated OR
----------
Every city in Every State:
FL has the city Jacksonville
CA has the city San Francisco
MI has the city Detroit
OR has the city Portland
NY has the city New York
----------
Do Both at Once:
California state is abbreviated CA and has city San Francisco
Michigan state is abbreviated MI and has city Detroit
New York state is abbreviated NY and has city New York
Florida state is abbreviated FL and has city Jacksonville
Oregon state is abbreviated OR and has city Portland
----------

.get() examples


In [36]:
# ex 39 Python the Hard Way modified code continued ...

# safely get a abbreviation by state that might not be there
state = states.get('Texas')
if not state:
    print("Sorry, no Texas.")

# get a city with a default value
city = cities.get('TX', 'Does Not Exist')
print("The city for the state 'TX' is: %s" % city)
print("The city for the state 'FL' is: %s" % states.get('Florida'))

city2 = states.get('Hawaii')
print("city2: %s" %city2)
if city2 == None:
    city2 = 'Value == None'
elif city2 == '':
    city2 = 'Value is empty ""'
elif not city2:
    city2 = 'Value Missing (Passed not test)'
else:
    city2 = 'No Such Value'
    
print("The city for the state 'HI' is: %s" % city2) 
print("These commands used .get() to safely retrieve a value")


Sorry, no Texas.
The city for the state 'TX' is: Does Not Exist
The city for the state 'FL' is: FL
city2: None
The city for the state 'HI' is: Value == None
These commands used .get() to safely retrieve a value

In [37]:
# more tests on above code from Learn Python the Hard Way:
print(not city2)

# tests that produce an error - numerical indexing has no meaning in dictionaries:
# print states[1][1]


False

In [38]:
# what happens if all keys are not unique?
foods = {
    'fruit': 'banana',
    'fruit': 'apple',
    'meat': 'beef'
}

for foodType, indivFood in foods.items():
    print("%s includes %s" % (foodType, indivFood))

# answer:  does not happen.  2nd attempt to use same key over-writes the first
# remove elements from a dictionary
del foods['meat']
# add an element to dictionary
foods['vegetables'] = 'carrot'
foods['meats'] = 'chicken'
# change an element to a dictionary
foods['vegetables'] = 'corn'

foods


fruit includes apple
meat includes beef
Out[38]:
{'fruit': 'apple', 'meats': 'chicken', 'vegetables': 'corn'}

In [39]:
# from MIT Big Data Class:
# Associative Arrays ==> Called "Dictionaries" or "Maps" in Python
# each value has a key that you can use to find it - { Key:Value }

super_heroes = {'Spider Man' : 'Peter Parker',
                'Super Man' : 'Clark Kent',
                'Wonder Woman': 'Dianna Prince',
                'The Flash' : 'Barry Allen',
                'Professor X' : 'Charles Exavior',
                'Wolverine' : 'Logan'}
print("%s %s" %("len(super_heroes): )", len(super_heroes)))
print("%s %s" %("Secret Identity for The Flash:", super_heroes['The Flash']))
del super_heroes['Wonder Woman']
print("%s %s" %("len(super_heroes): )", len(super_heroes)))
print(super_heroes)
super_heroes['Wolverine'] = 'John Logan'
print("Secret Identify for Wolverine:", super_heroes.get("Wolverine"))
print("Keys ... then Values (for super_heroes):")
print(super_heroes.keys())
print(super_heroes.values())


len(super_heroes): ) 6
Secret Identity for The Flash: Barry Allen
len(super_heroes): ) 5
{'Spider Man': 'Peter Parker', 'The Flash': 'Barry Allen', 'Super Man': 'Clark Kent', 'Wolverine': 'Logan', 'Professor X': 'Charles Exavior'}
('Secret Identify for Wolverine:', 'John Logan')
Keys ... then Values (for super_heroes):
['Spider Man', 'The Flash', 'Super Man', 'Wolverine', 'Professor X']
['Peter Parker', 'Barry Allen', 'Clark Kent', 'John Logan', 'Charles Exavior']

nested dictionaries


In [41]:
# list of dictionaries:

FoodList = [foods, {'meats':'beef', 'fruit':'banana', 'vegetables':'broccoli'}]
print(FoodList[0])
print(FoodList[1])


{'vegetables': 'corn', 'fruit': 'apple', 'meats': 'chicken'}
{'vegetables': 'broccoli', 'fruit': 'banana', 'meats': 'beef'}

In [42]:
# dictionary of dictionaries (sometimes called "nested dictionary"):
# note: this is an example only.  In real world, sinde FoodList is inclusive of foods, you probably would not include both
#       uniform structures (same number of levels across all elements) is also advisable if possible

nestedDict = { 'heroes':super_heroes, 'foods': foods, 'complex_foods':FoodList } 
print(nestedDict['heroes'])
print('-'*72)
print(nestedDict['complex_foods'])


{'Spider Man': 'Peter Parker', 'The Flash': 'Barry Allen', 'Super Man': 'Clark Kent', 'Wolverine': 'John Logan', 'Professor X': 'Charles Exavior'}
------------------------------------------------------------------------
[{'vegetables': 'corn', 'fruit': 'apple', 'meats': 'chicken'}, {'vegetables': 'broccoli', 'fruit': 'banana', 'meats': 'beef'}]

Working With Dictionaries and Nested Dictionaries - Helpful Code

This section has additional resources for working with dictionaries and nested dictionaries:

Sorting


In [43]:
# Help on Collections Objects including Counter, OrderedDict, dequeu, etc:
#  https://docs.python.org/2/library/collections.html

# regular dictionary does not necessarily preserve order (things added in randomly?)
# original order of how you add elements is prserved in OrderedDict

from collections import OrderedDict

myOrdDict = OrderedDict({'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2})
print(myOrdDict)
myOrdDict['pork belly'] = 7
print(myOrdDict)
myOrdDict['sandwich'] = 5
print(myOrdDict)
myOrdDict['hero'] = 5
print(myOrdDict)


OrderedDict([('orange', 2), ('pear', 1), ('banana', 3), ('apple', 4)])
OrderedDict([('orange', 2), ('pear', 1), ('banana', 3), ('apple', 4), ('pork belly', 7)])
OrderedDict([('orange', 2), ('pear', 1), ('banana', 3), ('apple', 4), ('pork belly', 7), ('sandwich', 5)])
OrderedDict([('orange', 2), ('pear', 1), ('banana', 3), ('apple', 4), ('pork belly', 7), ('sandwich', 5), ('hero', 5)])

In [45]:
# sorting the ordered dictionary ...

# dictionary sorted by key
# replacing original OrderedDict w/ results
myOrdDict = OrderedDict(sorted(myOrdDict.items(), key=lambda t: t[0]))
print("myOrdDict (sorted by key):\n %s" %myOrdDict)

# dictionary sorted by value
myOrdDict2 = OrderedDict(sorted(myOrdDict.items(), key=lambda t: t[1]))
print("myOrdDict2 (sorted by value):\n %s" %myOrdDict2)

# dictionary sorted by length of the key string
myOrdDict3 = OrderedDict(sorted(myOrdDict.items(), key=lambda t: len(t[0])))
print("myOrdDict3 (sorted by length of key):\n %s" %myOrdDict3)


myOrdDict (sorted by key):
 OrderedDict([('apple', 4), ('banana', 3), ('hero', 5), ('orange', 2), ('pear', 1), ('pork belly', 7), ('sandwich', 5)])
myOrdDict2 (sorted by value):
 OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4), ('hero', 5), ('sandwich', 5), ('pork belly', 7)])
myOrdDict3 (sorted by length of key):
 OrderedDict([('hero', 5), ('pear', 1), ('apple', 4), ('banana', 3), ('orange', 2), ('sandwich', 5), ('pork belly', 7)])

In [46]:
# collections.OrderedDict(sorted(dictionary.items(), reverse=True))
# pd.Series(OrderedDict(sorted(browser.items(), key=lambda v: v[1])))

# changing sort order to reverse key sort:
myOrdDict3 = OrderedDict(sorted(myOrdDict.items(), reverse=True))
print("myOrdDict3 (reverse key sort):\n %s" %myOrdDict3)


myOrdDict3 (reverse key sort):
 OrderedDict([('sandwich', 5), ('pork belly', 7), ('pear', 1), ('orange', 2), ('hero', 5), ('banana', 3), ('apple', 4)])

In [12]:
# testing of above strategy ... usually works but encountered  cases where it failed for no known reason
# lambda approach may be more reliable:

import pandas as pd

# value sort as pandas series:
myOrdDict4 = pd.Series(OrderedDict(sorted(myOrdDict.items(), key=lambda v: v[1])))
print("myOrdDict4 (value sort / alternate method):\n %s" %myOrdDict4)


myOrdDict4 (value sort / alternate method):
 pear          1
orange        2
banana        3
apple         4
hero          5
sandwich      5
pork belly    7
dtype: int64

In [13]:
# value sort in reverse order:
myOrdDict5 = OrderedDict(sorted(myOrdDict.items(), key=lambda t: (-t[1],t[0])))
print("myOrdDict5 (sorted by value in reverse order):\n %s" %myOrdDict5)


myOrdDict5 (sorted by value in reverse order):
 OrderedDict([('pork belly', 7), ('hero', 5), ('sandwich', 5), ('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])

In [14]:
# Help on Collections Objects including Counter, OrderedDict, dequeu, etc:
#  https://docs.python.org/2/library/collections.html

# sample using a list:
# for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
#     cnt[word] += 1

from collections import Counter

cnt = Counter()
for num in myOrdDict.values():
    cnt[num] +=1
    
print(cnt)


Counter({5: 2, 1: 1, 2: 1, 3: 1, 4: 1, 7: 1})

In [15]:
# http://stackoverflow.com/questions/11089655/sorting-dictionary-python-3
# another approach proposed in 2013 on Stack Overflow (but this may have been newer than OrderdDict at the time)

''' Help topic recommends this approach:
pip install sortedcontainers

Then:

from sortedcontainers import SortedDict
myDic = SortedDict({10: 'b', 3:'a', 5:'c'})
sorted_list = list(myDic.keys())

'''
print("conda install sortedcontainers is available in Python 2.7 and 3.6 as of April 2017")


conda install sortedcontainers is available in Python 2.7 and 3.6 as of April 2017

In [16]:
# some dictionaries to work with ...

super_heroes  # created earlier


Out[16]:
{'Professor X': 'Charles Exavior',
 'Spider Man': 'Peter Parker',
 'Super Man': 'Clark Kent',
 'The Flash': 'Barry Allen',
 'Wolverine': 'John Logan'}

In [17]:
super_heroes['The Incredible Hulk'] = 'Bruce Banner'

In [18]:
super_heroes  # seems to alpha sort on keys anyway


Out[18]:
{'Professor X': 'Charles Exavior',
 'Spider Man': 'Peter Parker',
 'Super Man': 'Clark Kent',
 'The Flash': 'Barry Allen',
 'The Incredible Hulk': 'Bruce Banner',
 'Wolverine': 'John Logan'}

In [48]:
# quick case study exploring another means of reverse sorting (from Stack Overflow):
reversed_tst = OrderedDict(list(super_heroes.items())[::-1])
reversed_tst   # note how in this instance, we don't get what we expected
               # this example might not be advisable ...


Out[48]:
OrderedDict([('Professor X', 'Charles Exavior'),
             ('Wolverine', 'John Logan'),
             ('Super Man', 'Clark Kent'),
             ('The Flash', 'Barry Allen'),
             ('Spider Man', 'Peter Parker')])

In [51]:
# however ... if we combine methodologies:
reversed_tst = OrderedDict(sorted(super_heroes.items(), key=lambda v: v[1])[::-1])
reversed_tst   # now the values are in reverse order ...


Out[51]:
OrderedDict([('Spider Man', 'Peter Parker'),
             ('Wolverine', 'John Logan'),
             ('Super Man', 'Clark Kent'),
             ('Professor X', 'Charles Exavior'),
             ('The Flash', 'Barry Allen')])

In [52]:
# however ... if we combine methodologies:
reversed_tst = OrderedDict(sorted(super_heroes.items(), key=lambda k: k)[::-1])
reversed_tst   # now the keys are in reverse order ...


Out[52]:
OrderedDict([('Wolverine', 'John Logan'),
             ('The Flash', 'Barry Allen'),
             ('Super Man', 'Clark Kent'),
             ('Spider Man', 'Peter Parker'),
             ('Professor X', 'Charles Exavior')])

In [19]:
fruitDict = {3: 'banana', 4: 'pear', 1: 'apple', 2: 'orange'}
fruitDict   # dictionaries appear to alpha sort at least on output making it hard to spot the effects below


Out[19]:
{1: 'apple', 2: 'orange', 3: 'banana', 4: 'pear'}

In [20]:
# help on library:
# http://www.grantjenks.com/docs/sortedcontainers/sorteddict.html

# test sample code from Stack Overflow post:
from sortedcontainers import SortedDict
myDic = SortedDict({10: 'b', 3:'a', 5:'c'})
sorted_list = list(myDic.keys())

print(myDic)
print(sorted_list)


SortedDict(None, 1000, {3: 'a', 5: 'c', 10: 'b'})
[3, 5, 10]

In [21]:
fruitDict = SortedDict(fruitDict)
sorted_list = list(fruitDict.keys())

print(fruitDict)
print(sorted_list)


SortedDict(None, 1000, {1: 'apple', 2: 'orange', 3: 'banana', 4: 'pear'})
[1, 2, 3, 4]

As per the examples above ...

So when to do what?

  • OrderedDict: will store whatever you put into it in whatever order you first record the data (maintaing that order)
  • SortedDict: by default will alpha sort the data (over-riding original order) and maintain it for you in sorted order
  • Dict: Don't care about storing it in order? just sort and output the results without storing it in new container

**Final note: only SortedDict allows indexing by numerical order on the data (by-passing keys) under both Python 2.7 and 3.6 (as shown in the next section)

Find the nth element in a dictionary


In [40]:
# MIT Big Data included a demo of this type of index/access to a dictionary in a Python 2.7 notebook
# the code is organized in a try-except block here so it won't halt the notebook if converted to Python 3.6

def print_1st_keyValue(someDict):
    try:
        print(someDict.values()[0])            # only works in Python 2.7
    except Exception as ee:
        print(str(type(ee)) + ": " + str(ee))  # error from PY 3.6: 
                                               # <class 'TypeError'>: 'dict_values' object does not support indexing
    finally:
        try:
            print(someDict.keys()[0])          # only works in Python 2.7
        except Exception as ee:
            print(str(type(ee)) + ": " + str(ee))  # error from PY 3.6: 
                                                   # <class 'TypeError'>: 'dict_keys' object does not support indexing
                        
print_1st_keyValue(super_heroes)


Peter Parker
Spider Man

In [44]:
print_1st_keyValue(myOrdDict)  # run same test on ordered dictionaries
                               # failed in Python 3.6, worked in Python 2.7
                               # reminder:  syntax is orderedDict.values()[0], orderedDict.keys()[0]


2
orange

In [22]:
print_1st_keyValue(fruitDict)  # run same test on sorted dictionary - 
                               # this works in Python 3.6 and 2.7
                               # reminder:  syntax is sortedDict.values()[0], sortedDict.keys()[0]


apple
1

Dictionary Comprehensions


In [23]:
# dictionary comprehension
[ k for k in fruitDict if k > 2 ]


Out[23]:
[3, 4]

In [24]:
[ fruitDict[k] for k in fruitDict if k > 1 ]


Out[24]:
['orange', 'banana', 'pear']

In [25]:
newDict = { k*2:'fruit - '+fruitDict[k] for k in fruitDict if k > 1 and len(fruitDict[k]) >=6} 
print(newDict)
type(newDict)


{4: 'fruit - orange', 6: 'fruit - banana'}
Out[25]:
dict

keyDict object


In [26]:
class KeyDict(dict):
    def __missing__(self, key):
        #self[key] = key  # uncomment if desired behavior is to add keys when they are not found (w/ key as value)
        #this version returns the key that was not found
        return key

kdTst = KeyDict(super_heroes)
print(kdTst['The Incredible Hulk'])
print(kdTst['Ant Man'])   # value not found so it returns itself as per __missing__ over-ride


Bruce Banner
Ant Man

In [27]:
help(SortedDict)


Help on class SortedDict in module sortedcontainers.sorteddict:

class SortedDict(__builtin__.dict)
 |  SortedDict provides the same methods as a dict.  Additionally, SortedDict
 |  efficiently maintains its keys in sorted order. Consequently, the keys
 |  method will return the keys in sorted order, the popitem method will remove
 |  the item with the highest key, etc.
 |  
 |  Method resolution order:
 |      SortedDict
 |      __builtin__.dict
 |      __builtin__.object
 |  
 |  Methods defined here:
 |  
 |  __copy__ = copy(self)
 |  
 |  __delitem__(self, key)
 |      Remove ``d[key]`` from *d*.  Raises a KeyError if *key* is not in the
 |      dictionary.
 |  
 |  __init__(self, *args, **kwargs)
 |      SortedDict provides the same methods as a dict.  Additionally, SortedDict
 |      efficiently maintains its keys in sorted order. Consequently, the keys
 |      method will return the keys in sorted order, the popitem method will
 |      remove the item with the highest key, etc.
 |      
 |      An optional *key* argument defines a callable that, like the `key`
 |      argument to Python's `sorted` function, extracts a comparison key from
 |      each dict key. If no function is specified, the default compares the
 |      dict keys directly. The `key` argument must be provided as a positional
 |      argument and must come before all other arguments.
 |      
 |      An optional *load* argument defines the load factor of the internal list
 |      used to maintain sort order. If present, this argument must come before
 |      an iterable. The default load factor of '1000' works well for lists from
 |      tens to tens of millions of elements.  Good practice is to use a value
 |      that is the cube root of the list size.  With billions of elements, the
 |      best load factor depends on your usage.  It's best to leave the load
 |      factor at the default until you start benchmarking.
 |      
 |      An optional *iterable* argument provides an initial series of items to
 |      populate the SortedDict.  Each item in the series must itself contain
 |      two items.  The first is used as a key in the new dictionary, and the
 |      second as the key's value. If a given key is seen more than once, the
 |      last value associated with it is retained in the new dictionary.
 |      
 |      If keyword arguments are given, the keywords themselves with their
 |      associated values are added as items to the dictionary. If a key is
 |      specified both in the positional argument and as a keyword argument, the
 |      value associated with the keyword is retained in the dictionary. For
 |      example, these all return a dictionary equal to ``{"one": 2, "two":
 |      3}``:
 |      
 |      * ``SortedDict(one=2, two=3)``
 |      * ``SortedDict({'one': 2, 'two': 3})``
 |      * ``SortedDict(zip(('one', 'two'), (2, 3)))``
 |      * ``SortedDict([['two', 3], ['one', 2]])``
 |      
 |      The first example only works for keys that are valid Python
 |      identifiers; the others work with any valid keys.
 |  
 |  __iter__(self)
 |      Return an iterator over the sorted keys of the dictionary.
 |      
 |      Iterating the Mapping while adding or deleting keys may raise a
 |      `RuntimeError` or fail to iterate over all entries.
 |  
 |  __reduce__(self)
 |  
 |  __repr__(self)
 |  
 |  __reversed__(self)
 |      Return a reversed iterator over the sorted keys of the dictionary.
 |      
 |      Iterating the Mapping while adding or deleting keys may raise a
 |      `RuntimeError` or fail to iterate over all entries.
 |  
 |  __setitem__(self, key, value)
 |      Set `d[key]` to *value*.
 |  
 |  clear(self)
 |      Remove all elements from the dictionary.
 |  
 |  copy(self)
 |      Return a shallow copy of the sorted dictionary.
 |  
 |  items(self)
 |      Return a list of the dictionary's items (``(key, value)`` pairs).
 |  
 |  iteritems(self)
 |      Return an iterator over the items (``(key, value)`` pairs).
 |      
 |      Iterating the Mapping while adding or deleting keys may raise a
 |      `RuntimeError` or fail to iterate over all entries.
 |  
 |  iterkeys(self)
 |      Return an iterator over the sorted keys of the Mapping.
 |      
 |      Iterating the Mapping while adding or deleting keys may raise a
 |      `RuntimeError` or fail to iterate over all entries.
 |  
 |  itervalues(self)
 |      Return an iterator over the values of the Mapping.
 |      
 |      Iterating the Mapping while adding or deleting keys may raise a
 |      `RuntimeError` or fail to iterate over all entries.
 |  
 |  keys(self)
 |      Return a SortedSet of the dictionary's keys.
 |  
 |  peekitem(self, index=-1)
 |      Return (key, value) item pair at index.
 |      
 |      Unlike ``popitem``, the sorted dictionary is not modified. Index
 |      defaults to -1, the last/greatest key in the dictionary. Specify
 |      ``index=0`` to lookup the first/least key in the dictiony.
 |      
 |      If index is out of range, raise IndexError.
 |  
 |  pop(self, key, default=<object object>)
 |      If *key* is in the dictionary, remove it and return its value,
 |      else return *default*. If *default* is not given and *key* is not in
 |      the dictionary, a KeyError is raised.
 |  
 |  popitem(self, last=True)
 |      Remove and return a ``(key, value)`` pair from the dictionary. If
 |      last=True (default) then remove the *greatest* `key` from the
 |      diciontary. Else, remove the *least* key from the dictionary.
 |      
 |      If the dictionary is empty, calling `popitem` raises a
 |      KeyError`.
 |  
 |  setdefault(self, key, default=None)
 |      If *key* is in the dictionary, return its value.  If not, insert *key*
 |      with a value of *default* and return *default*.  *default* defaults to
 |      ``None``.
 |  
 |  update(self, *args, **kwargs)
 |      Update the dictionary with the key/value pairs from *other*, overwriting
 |      existing keys.
 |      
 |      *update* accepts either another dictionary object or an iterable of
 |      key/value pairs (as a tuple or other iterable of length two).  If
 |      keyword arguments are specified, the dictionary is then updated with
 |      those key/value pairs: ``d.update(red=1, blue=2)``.
 |  
 |  values(self)
 |      Return a list of the dictionary's values.
 |  
 |  viewitems(self)
 |      Return ``ItemsView`` of dictionary (key, value) item pairs.
 |  
 |  viewkeys(self)
 |      Return ``KeysView`` of dictionary keys.
 |  
 |  viewvalues(self)
 |      Return ``ValuesView`` of dictionary values.
 |  
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |  
 |  fromkeys(cls, seq, value=None) from __builtin__.type
 |      Create a new dictionary with keys from *seq* and values set to *value*.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from __builtin__.dict:
 |  
 |  __cmp__(...)
 |      x.__cmp__(y) <==> cmp(x,y)
 |  
 |  __contains__(...)
 |      D.__contains__(k) -> True if D has a key k, else False
 |  
 |  __eq__(...)
 |      x.__eq__(y) <==> x==y
 |  
 |  __ge__(...)
 |      x.__ge__(y) <==> x>=y
 |  
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __gt__(...)
 |      x.__gt__(y) <==> x>y
 |  
 |  __le__(...)
 |      x.__le__(y) <==> x<=y
 |  
 |  __len__(...)
 |      x.__len__() <==> len(x)
 |  
 |  __lt__(...)
 |      x.__lt__(y) <==> x<y
 |  
 |  __ne__(...)
 |      x.__ne__(y) <==> x!=y
 |  
 |  __sizeof__(...)
 |      D.__sizeof__() -> size of D in memory, in bytes
 |  
 |  get(...)
 |      D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
 |  
 |  has_key(...)
 |      D.has_key(k) -> True if D has a key k, else False
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes inherited from __builtin__.dict:
 |  
 |  __hash__ = None
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T


In [ ]: