Dictionaries, tuples, and a taste of object attributes ------------------------------------------------------ Dictionaries, tuples, lists, and strings are distinguished by the punctuation used to enclose them: {} # empty dictionary () # empty tuple [] # empty list "" # empty string In current Python 2.2, all these types are sequences, but in earlier versions, dictionaries were not considered sequences. Here are a list, a dictionary, a string, and a tuple with one element each: ["foo"] {"Seth": 22} "x" ("neat", ) # there is a special convention that a tuple with # one element still has to have a comma, to # distinguish it from simply putting some value in # parentheses Here are a list, a dictionary, a string, and a tuple with two elements each: ["foo", "bar"] {"Seth": 22, "Rebecca": 16} "xy" ("neat", "clean") I haven't talked about dictionaries or tuples yet. Tuples ------ A tuple is much like a list -- it's a simple sequence type. location = (2,-3) print location[0] print location[1] print location[2] print location That's pretty much all you should need to know about tuples for now. Almost all the syntax for manipulating them corresponds to the syntax for manipulating lists. For example, you can take slices in tuples. A useful trick with tuples is called "tuple packing" and "tuple unpacking": t = (1,2,3) # pack a, b, c = t # unpack So tuples are a quick and convenient way to represent a collection of values in a single object. They're often used when you want to collect several different values together but don't want to define your own class (a process we'll talk about in a different class). Of course, you can ask for the len of a tuple, or do "for x in some_tuple", or pretty much anything which would work with a list. Note that tuples and strings are "immutable" -- you can't modify their individual elements. This is the main difference between a tuple and a list -- once the tuple is created, it can't be changed (although you can certainly create a new and different tuple to store in place of your old tuple). One benefit of tuples relative to lists is that tuples, like strings, are "hashable"; lists are "unhashable". The benefit of hashability will be seen below. A dictionary is also called an associative array. (It corresponds to what Perl programmers call a "hash", although that term has a different meaning in computer science, so I usually avoid it.) Dictionaries allow you to associate data with other data, to create "mappings", "associations", "definitions", or "look-up tables". They can be extremely useful. These objects are called dictionaries by analogy with printed dictionaries, which have terms and associated definitions. If you know a term, you can consult a dictionary to learn its definition. Python dictionaries let you do much the same thing. orgs = {"EFF": "Electronic Frontier Foundation", "CDT": "Center for Democracy and Technology", "ACLU": "American Civil Liberties Union", "EPIC": "Electronic Privacy Information Center", "CDR": "Campaign for Digital Rights", "MPAA": "Motion Picture Association of America", "RIAA": "Recording Industry Association of America", "BSA": "Business Software Alliance", "AAP": "Association of American Publishers", "NSA": "National Security Agency"} print orgs["NSA"] print orgs["EPIC"] print orgs["CIA"] Dictionaries are mutable (and, you might conclude correctly, unhashable). You can store new "definitions" into a dictionary: orgs["CCIA"] = "Computer and Communications Industry Association" print orgs["CCIA"] The values I called "terms" above are officially known as "keys" (so the keys in the dictionary given in the example are "EFF", "CDT", etc.). The values I referred to as the "definitions" of the keys -- the values which are associated with the keys and which can be retrieved by consulting the dictionary -- are called "values". The things stored in a dictionary are frequently called "key-value pairs" or "key-value mappings". You could also call them "definitions", if you like. The keys and values in the examples above are both strings, but they need not be. squares = {} for i in range(100): squares[i] = i*i print squares[30] print squares[300] You can get a list of the keys stored within a dictionary by using the dictionary's keys() method: print squares.keys() Similarly, you can get a list of the values by using the values() method: print squares.values() A common idiom for printing out the entire contents of a dictionary looks something like this: for k in dict.keys(): print k, dict[k] There's also a has_keys() method within each dictionary object. print orgs.has_key("NSA") print orgs.has_key(12) print squares.has_key("NSA") print squares.has_key(12) This will tell you in advance whether a dictionary contains a particular key. (If you try to access a non-existent key within a dictionary, you'll get an error.) The values in a dictionary may be of any data type whatsoever, including data types we have not yet mentioned. (Remember that functions are objects in Python, so you can store functions within lists or dictionaries or certain other objects.) The keys in a dictionary need not all be the same type. For example, you can store values using both numeric keys and string keys. However, the keys must all be of "hashable" types. Hashable types include all numeric types, tuples, strings, functions, user-defined classes, and instances of user-defined classes; they exclude lists and dictionaries.