Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
Python dictionaries - mutable and immutable keys and values
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)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesY111 - Python - More on Collections and Sequences 
Mutable v Immuatble objects in Python, and the implication - (2015-02-24) 
Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl - (2015-01-18) 
zip in Python - (2012-07-05) 
Python for loops - applying a temporary second name to the same object - (2011-09-14) 
List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06) 
Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12) 
Sorting - naturally, or into a different order - (2010-08-14) 
Sorting people by their names - (2010-07-29) 
Python - access to variables in the outer scope - (2010-04-12) 
List Comprehensions in Python - (2008-11-06) 
Anonymous functions (lambdas) and map in Python - (2008-11-04) 
Callbacks - a more complex code sandwich - (2007-08-19) 
Last elements in a Perl or Python list - (2007-08-16) 
Python - extend v append on a list - (2006-10-20) 
Copying a reference, or cloning - (2006-03-05) 
What is a callback? - (2005-07-22) 
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.
4750 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).