question about list extension
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Fri Apr 16 10:23:04 EDT 2010
J a écrit :
> Ok... I know pretty much how .extend works on a list... basically it
> just tacks the second list to the first list... like so:
>
>>>> lista=[1]
>>>> listb=[2,3]
>>>> lista.extend(listb)
>>>> print lista;
> [1, 2, 3]
>
> what I'm confused on is why this returns None:
> So why the None? Is this because what's really happening is that
> extend() and append() are directly manipulating the lista object and
> thus not actuall returning a new object?
Exactly.
> Even if that assumption of mine is correct, I would have expected
> something like this to work:
>
>>>> lista=[1]
>>>> listb=[2,3]
>>>> print (lista.extend(listb))
> None
So what ? It JustWork(tm). list.extend returns None, so "None" is
printed !-)
(snip)
> So changing debugger.printout() to:
>
> def printout(self,*info):
>
> lets me pass in multiple lists... and I can do this:
>
> for tlist in info:
> for item in tlist:
> print item
>
> which works well...
>
> So, what I'm curious about, is there a list comprehension or other
> means to reduce that to a single line?
Why do you think the above code needs to be "reduced to a single line" ?
What's wrong with this snippet ???
> It's more of a curiosity thing at this point... and not a huge
> difference in code... I was just curious about how to make that work.
Uh, ok.
What about:
from itertools import chain
def printout(*infos):
print "\n".join("%s" % item for item in chain(*infos))
HTH
More information about the Python-list
mailing list