Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
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 articles
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)


Back to
Looking back at www.wellho.net
Previous and next
or
Horse's mouth home
Forward to
Disassembling Python and Java - previously compiled code
Some other Articles
Changing a class later on - Ruby
Points West to Belfast
Jargon busting
Disassembling Python and Java - previously compiled code
Python dictionaries - mutable and immutable keys and values
Looking back at www.wellho.net
OpenGL / C / C++ - an example to get you started
Hotel star ratings - towards a better system of review
Strings in C
Structures v Structure Pointers in C. How, which, why.
4350 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 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. 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3150_Pyt ... alues.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat