Do this as a list comprehension?

Mensanator mensanator at aol.com
Wed Jun 11 01:45:59 CEST 2008


On Jun 10, 6:09 pm, Lie <Lie.1... at gmail.com> wrote:
> On Jun 8, 11:11 pm, Mensanator <mensana... at aol.com> wrote:
>
>
>
>
>
> > On Jun 8, 4:04 am, Lie <Lie.1... at gmail.com> wrote:
>
> > > On Jun 8, 8:56 am, Mensanator <mensana... at aol.com> wrote:
>
> > > > On Jun 7, 8:22�pm, John Salerno <johnj... at gmailNOSPAM.com> wrote:
>
> > > > > Mensanator wrote:
> > > > > > What I DID say was that how the builtins actually
> > > > > > work should be understood and it APPEARED that the
> > > > > > OP didn't understand that. Maybe he understood that
> > > > > > all along but his example betrayed no evidence of
> > > > > > that understanding.
>
> > > > > Well, the truth is that I know zip truncates to the shorter of the two
> > > > > arguments,
>
> > > > Ok, sorry I thought otherwise.
>
> > > > > and also in my case the two arguments would always be the
> > > > > same length.
>
> > > > Yes, because you're controlling the source code.
> > > > But since lists are mutable, source code literals
> > > > don't always control the length of the list.
>
> > > Since when source code literals ever control the length of a list?
>
> > Isn't score_costs = [0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3]
> > considered a literal?
>
> Yep, but it's not the sole controller of the length of a list. There
> are other things that might control the length of the list like del,
> append, etc.

I believe I just said that. Perhaps I should have said
at the instant it's created, the length is determined
by the literal.

Remember,

score_costs = [0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3]

is preceeded by range(11), so of course AT THAT INSTANT,
the lengths match. But, as you say, there's no guarantee
that the list lengths will remain unchanged.

And that's why I'm saying it isn't necessarily a good idea
to assume that. For the way it's used, the zip function
IS assuming that.

>
> > > What controls the length of the list is the semantic meaning of the
> > > list,
>
> > Wha do you mean by that? The list contains 11 objects.
> > How could the length be any different?
>
> What I meant is in some cases (not all) the list might semantically be
> nonsense if it is of different length (i.e. it have fixed length).

Sure, but remember, the OP was asking how to do this in a
list comprehension where he doesn't have the option of
iterating through both lists like he does with zip.

When I mentioned that could be solved by enumerate, I said
it simultaneously guaratees that the index numbers automatically
end up the same length as the target list and avoids the
hypothetical case where the list lengths somehow don't match.

Of course nothing can be done if the actual list length is
semantically nonsense and you actually might need to exploit
the truncating of the list to a fixed range. But there are
many cases where you DON'T want that to happen (if you deposit
5 checks at the bank, you certainly want credit for ALL of them,
not just some idiot's notion that only 4 can be deposited in
a single transaction.)

>
> > > in some cases it just makes no sense that the list would ever
> > > have different length.
>
> > And in such case there won't be any problem, will there?
>
> > Is that a good habit to teach a newbie? To write
> > code that only works for special cases?
>
> I think it is up to the programmer to decide whether special case is
> enough or a general case is necessary.

Yes, he can certainly decide. Provided he knows what all the
options are. There's no way to tell if the OP understands
what the options are. I say it's always better to supply too
much information than not enough. Assume the reader is clever
enough to seperate the wheat from the chaff. If it turns out
he's not, then I at least _I_ will be blameless.




More information about the Python-list mailing list