question about list extension

J

J

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:
lista=[1]
listb=[2,3]
print lista.extend(listb) None
print lista
[1, 2, 3]

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?

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

The reason this is bugging me right now is that I'm working on some
code. My program has a debugger class that takes a list as it's only
(for now) argument. That list, is comprised of messages I want to
spit out during debug, and full output from system commands being run
with Popen...

So the class looks something like this:

class debugger():
def printout(self,info):
for line in info:
print "DEBUG: %s" % line


and later on, it get's called like this

meminfo = Popen('cat
/proc/meminfo',shell=True,stdout=PIPE).communicate[0].splitlines()

if debug:
debugger.printout(meminfo)

easy enough....

BUT, what if I have several lists I want to pass on multiple lists...

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?

I tried this, which didn't work because I'm messing up the syntax, somehow:

def func(*info)
print [ i for tlist in info for i in tlist ]

so can someone help me understand a list comprehension that will turn
those three lines into on?

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.

Cheers,

Jeff
 
B

Bruno Desthuilliers

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
 
L

Lie Ryan

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:
lista=[1]
listb=[2,3]
print lista.extend(listb) None
print lista
[1, 2, 3]

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?

In python every function that does not explicitly returns a value or use
a bare return returns None. So:

def foo():
pass
def bar():
return

print foo()
print bar()

you can say that returning None is python's equivalent to void return
type in other languages.
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

Why would these has to be different?
print None
print (None)

So, what I'm curious about, is there a list comprehension or other
means to reduce that to a single line?

from itertools import chain
def printout(*info):
print '\n'.join(map(str, chain(*info)))

or using generator comprehension

from itertools import chain
def printout(*info):
print '\n'.join(str(x) for x in chain(*info))
 
J

J. Cliff Dyer

On 04/16/10 23:41, J wrote:

from itertools import chain
def printout(*info):
print '\n'.join(map(str, chain(*info)))

or using generator comprehension

from itertools import chain
def printout(*info):
print '\n'.join(str(x) for x in chain(*info))


It's even easier if you don't need to modify lista.

print lista + listb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top