Jumping around when assigning elements

Francis Avila francisgavila at yahoo.com
Mon Dec 15 18:29:57 EST 2003


Matthew Sims wrote in message
<1e963607.0312151408.583221e6 at posting.google.com>...
>Python Newbie here. This is my first time learning object-oriented
>programming and trying to break out of the usual Korn/Perl/PHP style
>of programming. Having some difficulty understand some items.
>
>For lists, I understand this:
>C=["need","some","help"]
>print C[1]
>some
>
>But I can't seem to do this:
>C[3]="here"
>
>I know about C.append("here") but this brings me to my question...
>
>Is there anyway to assign to an element that wasn't defined in the
>beginning? Like if I wanted element 5 assigned but not element 4
>without using "" or None?

I assume you tried 'insert'?
>>> C=["need","some","help"]
>>> C.insert(10, 'here')
>>> C
['need', 'some', 'help', 'here']

Ah, well that didn't work.  Python has no (builtin) way to do what you want.
The reason isn't technical, but that the list needs *something* in that
intervening space, and Python isn't about to make rash presumptions about
what you want there ("explicit is better than implicit").  In choosing
between two behaviors for inserting to an index beyond the end of a list,
appending was deemed less surprising, less violent, and less likely to
introduce mysterious bugs than filling in the intermediate elements with
who-knows-what.

I can't even think of why one would want lists to have that sort of
behavior.  It seems to me that if we're assigning to arbitrary elements and
we want the list to grow with it, that we should be using a different data
structure, like a dictionary with integer keys.  The index of a list
generally has no semantic correlation to the data contained therein--it just
specifies order, and the data doesn't care about its own index.  If we want
a semantic correlation, we make that correlation explicit by a dictionary's
key-value pair.

If you want to do this sort of thing (implicit assignment to intermediate
elements in list insertions), write a function to do it, or subclass list.
You can even modify the slice behavior itself (if you go the subclass
route), so that C[3] will silently act like perl instead of Python, but this
is *quite* unPythonic. (Other Python people reading your code will be
confused.)

If this sort of answer surprises you (coming from perl, it might...), do an
"import this" at an interactive prompt and read. :)

>I'm currently re-writing a Perl script into Python and with Perl I was
>free to assign any element in the array without having to fill in the
>previous elements. I can't seem to do that in Python...unless I'm
>doing it wrong.

Again, in general, Python has *no* implicit
assignments/declarations/modifications/etc.  If you don't *explicitly* ask a
list to fill in its intermediate elements, Python won't do it.  This is a
conscious design decision, and a great part of what makes Python Python.
For at least two senses of "great", IMO.

I could never learn perl (despite many attempts), so I ask, what does perl
do to the intermediate elements?  Is a perl array more like a dictionary
with implicit integer keys (so intermediate keys simply don't exist), or
does it fill in the intermediate elements with 0 or something like that?  In
what perl idioms is this behavior useful?  (I still mean to learn perl one
of these days, for shell scripting.)
--
Francis Avila





More information about the Python-list mailing list