# some ways to translate between different collections: # dictionary, list, and set # a dictionary can be represented as tuples of two items >>> d = { 'a':13, 'b':4, 'c':245 } >>> d Out[49]: {'a': 13, 'b': 4, 'c': 245} >>> d.items() Out[50]: [('a', 13), ('c', 245), ('b', 4)] # so a list can be used to create a dictionary >>> l = [('h', 1), ('z', 2), ('a', 3)] >>> l Out[51]: [('h', 1), ('z', 2), ('a', 3)] >>> dict(l) Out[52]: {'a': 3, 'h': 1, 'z': 2} # parallel lists can be turned into a dictionary easily >>> a = [ 1, 2, 3, 4 ] >>> b = [ 5, 6, 7, 8 ] >>> zip(a, b) Out[56]: [(1, 5), (2, 6), (3, 7), (4, 8)] >>> dict(zip(a, b)) Out[57]: {1: 5, 2: 6, 3: 7, 4: 8} # a set can be made from any collection (with immutable items) >>> set('hello') Out[58]: set(['h', 'e', 'l', 'o']) >>> set(l) Out[59]: set([('z', 2), ('a', 3), ('h', 1)]) >>> set(d.values()) Out[60]: set([4, 13, 245]) >>> d['c'] = 13 >>> set(d.values()) Out[61]: set([4, 13])