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:
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?
Even if that assumption of mine is correct, I would have expected
something like this to work:
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
just tacks the second list to the first list... like so:
[1, 2, 3]lista=[1]
listb=[2,3]
lista.extend(listb)
print lista;
what I'm confused on is why this returns None:
[1, 2, 3]lista=[1]
listb=[2,3]
print lista.extend(listb) None
print lista
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:
Nonelista=[1]
listb=[2,3]
print (lista.extend(listb))
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