curly-brace-aphobic?

D-Man dsh8290 at rit.edu
Tue Jan 30 11:02:11 EST 2001


On Tue, Jan 30, 2001 at 01:38:37AM +0000, Remco Gerlich wrote:
| D-Man <dsh8290 at rit.edu> wrote in comp.lang.python:
| > On Mon, Jan 29, 2001 at 08:35:42AM -0600, Grant Griffin wrote:
| > | C.Laurence Gonsalves wrote:
| > | I guess it's because sequences and dictionaries are conceptually very
| > | different.  (You can tell, because we have a special word just to
| > 
| > I'm not sure how different they are conceptually.  A dictionary is a
| > collection (sequence) of data that is indexed by arbitrary keys.  A
| > list is a collection (sequence) of data that is indexed by integers
| > only.
| 
| An essential difference is the fact that lists are ordered, but dicts aren't.

In my Discrete Mathematics II class we discussed Partial Ordering
quite a bit.  If you take this perspective, then lists aren't really
orderd any more than a dict is.

A Partial Ordering is formed by creating a mapping between the
objects you have and a set of objects that already have a known
partial ordering (often the set of integers).  In this view, a list is
simply a mapping from the integers to arbitrary objects and vice
versa.   So sorting a list is really nothing more than altering you
concept of that mapping.

If you write :

for key in dict.keys() :
	pass

you have now ordered your dictionary by creating a mapping from
elements (the keys) in the dictionary to the set of integers (the list
of keys).  What this ordering (or any ordering for that matter) means
is up to the person making use of the ordering.  You can also write

for item in dict.items() :
	pass

and create a possibly (probably!) different ordering for the
dictionary.  In practice, to say that something is "unordered" really
means that there is no (universally) meaningful ordering for the data.
If you take some dictionaries and print the contents using either of
the above loops, you are not guaranteed to get the results in the same
order each time.  In this sense, yes, a dict is unordered.

Suppose I use a dict with integers as keys.  How is this different from a list?

dict = { 1:"one" , 2:"two" , 3:"three" }
list = [   "one" ,   "two" ,   "three" ]

for i in len( list ) :
	print list[i]

for i in len( dict ) :
	print dict[i]

In this case, they are identical!  For all we know, a list is
implemented as a dictionary (we know the interface is a little
different but we don't know the implementation).  (Ok, so I know that
Python doesn't implement them the same way, but that is irrelevant.) 

I still maintain my postiion that a list is really a special case of a
dictionary, on a theoretical level.  I'm not saying that your points
are incorrect or invalid or anything like that.  At the practical
level I don't really use/consider lists and dicts as the same thing.
I am sure that there are also efficiency issues to deal with when
implementing lists and dicts.  The list can be optimized since it
knows that only integers will be used and of the integers, only the
integers in the range 0 <= n <= len( list ).

| 
| So lists have things like append(), and dicts can set items that don't exist
| yet. Sorting a list makes sense, sorting a dict doesn't. Et cetera.
| 
| But indexing should be the same for lists, dicts, tuples, shelves, arbitrary
| instances - it's simply indexing. It does the same thing.
| 
| It's almost odd that list/dict indexing is different from indexing a module
| or instance...

I'm not familiar with indexing a module or instance.  What exactly
does it mean?

| 
| -- 
| Remco Gerlich

enjoying-this-friendly-discussion-ly y'rs -D





More information about the Python-list mailing list