Please provide a better explanation of tuples and dictionaries

Rick Johnson rantingrickjohnson at gmail.com
Sun Feb 3 06:20:48 CET 2013


############################################################
#                 Quote: Daniel Rouse Jr.                  #
############################################################
# To me, this looks like an array. Is tuple just the       #
# Python name for an array?                                #
############################################################

The problem with understanding Python collections is directly due to improper naming. 

First let's consider the sequence types "list" and "tuple". For the most part lists and tuples are exactly the same. They are both containers for holding values. 

GvR wisely choose to borrow the English word "list" over the esoteric CS term "array", but he /unwisely/ choose to borrow the maths term "tuple" over something more self-documenting to describe what is basically an immutable list.

Even someone who has no programming experience could most probably intuit what a "Python list" /is/. Everyone has made a grocery list, or a "to-do" list. The transformation from a tangible object like: *a linear list of items written on paper* to an intangible object like: *a Python list holding N objects* is not very difficult fathom. HOWEVER, then along comes the "seemingly" innocent tuple with fiery red hair and that devilish little grin intent on screwing up the whole logical flea circus!

Now you may ask yourself: 

  WHAT THE HELL IS A TUPLE? AND WHERE DID THIS ESOTERIC TERM ORIGINATE! 

And if you ask the oracle (aka: Google) you might get this answer:

*Google Said:* /"""In mathematics and computer science, a tuple is an ordered list of elements. In set theory, an (ordered) n-tuple is a sequence (or ordered list) of n elements, where n is a non-negative integer. """"/

Okay google, so a tuple is an /ordered set/ and a list is an /ordered collection/, got it, (and thanks GvR for the mental overload!) however the names fail to convey this VERY important piece of information 

For the fix, it would seem logical to simply extend the term "list" in a manner that will convey a "set" relationship. "DynamicList" and "StaticList" fit the bill HOWEVER these terms are FAR to verbose to use on a daily basis!  Now, we could naively use "list" for an ordered collection, and "staticlist" for an ordered set, HOWEVER even this is a foolish choice!

The final solution is NOT two different types with verbose names, NO, the solution is ONE ordered collection type with a method to convert it into an ordered set. Observe:

py> list = list()
py> list.extend([1,2,3])
[1,2,3]
py> list.append('logical')
[1,2,3,'logical']
py> staticList = list.freeze()
py> staticList[-1]
'logical'
py> staticList.append('error')
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    staticList.append('error')
AttributeError: 'StaticList' object has no attribute 'append'

*school-bell*



More information about the Python-list mailing list