Lists, Tuples, and dictionaries are the conventional collection variables in Python - but when you stop to consider it, objects and strings are collections too. All of these structures bundle together other elements (members) in various ways.
In first dictionary demonstrations, we usually use strings as both the key and the value; strings can be quickly set up so the demos are short, and strings are far and away the most common keys anyway. There's such a first example from our training notes
[here], and an example that goes a lot further in accessing / sorting all the elements (you can't sort a dictionary, but you can sort the keys!)
[here].
During yesterday's Python course in Oxford, I set (and later answered) an exercise that created a dictionary of objects, keyed on a string - the code is
[here]. That's a very common use indeed of a dictionary, where a whole series of things (objects) is reference by some sort of unique string - a "primary key" in database terms.
A further example, in which the keys are other objects, may be found
[here], with the class that's used as keys defined in a separate file
[here]. You'll note that we've added two objects set up with identical parameters as keys, and they have formed separate members of the dictionary. That's because objects of our type coin are "muttable" - i.e. changeable, as opposed to strings which are not changeable in situ - "immutable"; if you use the same immutable key twice, you'll be referring to one and the same object and the second one added will overwrite the first.
More about mutable and immutable on my next Python courses - you may have missed Oxford this week, and the course in Frankfurt in 2 weeks time is private (single company) one. But we do have a public course coming up in about a month. See
Learning to Program in Python and
Python Programming.
(written 2011-01-29, updated 2011-02-10)
20eb
Associated topics are indexed under
Y111 - Python - More on Collections and Sequences [3797] zip in Python - (2012-07-05)
[3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
[3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[2894] Sorting people by their names - (2010-07-29)
[2718] Python - access to variables in the outer scope - (2010-04-12)
[1873] List Comprehensions in Python - (2008-11-06)
[1869] Anonymous functions (lambdas) and map in Python - (2008-11-04)
[1310] Callbacks - a more complex code sandwich - (2007-08-19)
[1304] Last elements in a Perl or Python list - (2007-08-16)
[899] Python - extend v append on a list - (2006-10-20)
[633] Copying a reference, or cloning - (2006-03-05)
[386] What is a callback? - (2005-07-22)
[61] Python is a fabulous language - (2004-09-24)
Some other Articles
Changing a class later on - RubyPoints West to BelfastJargon bustingDisassembling Python and Java - previously compiled codePython dictionaries - mutable and immutable keys and valuesLooking back at www.wellho.netOpenGL / C / C++ - an example to get you startedHotel star ratings - towards a better system of reviewStrings in CStructures v Structure Pointers in C. How, which, why.