This is now an archive web site. Some is still relevant as at May 2026 but some is purely of historic interest.
Lisa and I (Graham) are now fully retired from IT training.We have made many friends over 30 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Apache, Linux and Solaris/SunOS too. Our training notes are out of date, but with upward compatability some examples remain operational and relevant. You are welcome to make use of them "as seen", at your own risk. We now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We remain active, enjoying the times that we are retired but still healthy enough in mind and body to do things!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
|
Python dictionaries - mutable and immutable keys and values (written 2011-01-29)
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 articles Y111 - Python - More on Collections and Sequences [61] Python is a fabulous language - (2004-09-24) [386] What is a callback? - (2005-07-22) [633] Copying a reference, or cloning - (2006-03-05) [899] Python - extend v append on a list - (2006-10-20) [1304] Last elements in a Perl or Python list - (2007-08-16) [1310] Callbacks - a more complex code sandwich - (2007-08-19) [1869] Anonymous functions (lambdas) and map in Python - (2008-11-04) [1873] List Comprehensions in Python - (2008-11-06) [2718] Python - access to variables in the outer scope - (2010-04-12) [2894] Sorting people by their names - (2010-07-29) [2920] Sorting - naturally, or into a different order - (2010-08-14) [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12) [3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06) [3439] Python for loops - applying a temporary second name to the same object - (2011-09-14) [3797] zip in Python - (2012-07-05) [4398] Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl - (2015-01-18) [4442] Mutable v Immuatble objects in Python, and the implication - (2015-02-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.
|
4759 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, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
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. So much so, and it's so long ago
that we are retired
Link to Ezine home page (for reading).
|
|