Rant (was Re: x*x if x>10

Diez B. Roggisch deets at nospam.web.de
Sun Jul 27 18:40:00 CEST 2008


DaveM schrieb:
> On Sun, 27 Jul 2008 16:57:14 +0200, "Diez B. Roggisch" <deets at nospam.web.de>
> wrote:
> 
>> Marc 'BlackJack' Rintsch schrieb:
>>> On Sun, 27 Jul 2008 16:41:19 +0200, Diez B. Roggisch wrote:
>>>
>>>> DaveM schrieb:
>>>>> Getting back to the list concatenation, I finally found the itertools.chain 
>>>>> command which is the most compact and fastest (or second fastest by a 
>>>>> trivial amount, I can't remember which). Along the way, I must have 
>>>>> tried/used half a dozen methods, ...which brings me back my initial PERL 
>>>>> comment. There's more than one way to do it in Python, too.
> 
>>>> But I *do* know that taking the python zen literally is fruitless.
> 
>>> I think it should be taken more literally than the wrong reduction to 
>>> "there should be only one way".  People tend to forget "obvious" and 
>>> "preferably" all the time.
> 
>> Good point. The OP found the obvious way of extending. I wonder what his 
>> reasons were to abandon it.
> 
> You'll have guessed, I'm sure, that I'm not a professional programmer. This
> was the third rewrite of a program to match candidate groups to examiners on
> a three day course I run, necessitated on this occasion by a change in the
> structure of the course. I originally learnt python as I wrote, and some of
> the early code was ugly and verbose, so once the current rewrite was working
> I took the opportunity to tidy the code up and document it (yes, I know, but
> as I said, I'm an amateur). The list concatenation was an itch I couldn't
> scratch:
> 
>     temp = []
>     for value in sessexam.values():
>         temp.extend(value)
>     c_exam = [name for name in set(temp)] #See what I mean about verbose?
>     c_exam.sort()
>     return c_exam
> 
> Six lines just didn't feel like it ought to be the best way to do something
> so simple. I liked the attempt below better, but was foolish enough to time
> it, so that was the end of that.
> 
>     return sorted(list(set(reduce(lambda x, y: x+y, sessexam.values()))))
> 
> The current version (below) is a compromise, but I still feel there _ought_
> to be a simple one word command to join multiple lists.
> 
>     a = list(set(itertools.chain(*sessexam.values())))
>     a.sort() #As I write I'm wondering if I really need it sorted. Hmm...
>     return a

You are aware that the above is much more than "concatenate a bunch of 
lists?" You want unique values & sorting, which are two additional 
requirements. I'd say 3 lines of code for three requirements is ok, so

all = sum(sessexam.values(), [])
unique = set(all)
sorted_result = sorted(unique)

And obviously there are more ways to skin *that* cat, some (as 
itertools.chain) being less memory intensive:


a = sorted(set(itertools.chain(*sessexam.values())))

It's debatable if this one-liner is really the way to go, but I fail to 
see how you expect there to be a specialized builtin for this kind of task.


Diez



More information about the Python-list mailing list