[Tutor] summing lists
d at davea.name
Tue Apr 10 17:45:13 CEST 2012
On 04/10/2012 11:28 AM, Hs Hs wrote:
> sorry I did this following, please advise if any better way exist:
You top-posted, so we lose the context you were using.
You never really say why you're using lists with exactly one element in
them, but presuming that the real lists consist of something like
a = [4, "Tom Jones"]
b = [12, "Peter Rabbit", "March Hare"]
I'll follow your convention of assuming only the zeroth element is
relevant to your 'definition of add'.
> gc = lambda x,y: x+y
> atgc = lambda x,y,k,l: x+y+k+l
No point in using lambda for that purpose. Since you are not trying to
make it anonymous, you could simply use
def gc(x, y):
return x + y
But the more interesting question is how to combine the two functions,
and make one which can take a variable number of items.
return sum( [ x for x in many ] )
More information about the Tutor