cannot pass a variable from a function

Pierre-Frédéric Caillaud peufeu at free.fr
Fri Jun 18 16:56:15 EDT 2004


> if I use the following,  it only returns 1 value.
> tup1=((1,3),(2,5),(3,9))
> def NewFctn(c):
>     for a,b in c:
>         v=b*9
>         return v
> y=NewFctn(tup1)

	Well of course.
	Your function returns during the first iteration of the for loop.
	You need to put this return after the end of the for loop so the loop  
loops through all values in c.
	I don't really understand what you want so my suggestion could be bad.

Suggestion :

if you want to apply a function to all tuples in your list, use map :

map( lambda tup: (tup[0], tup[1]*9), tupl )

or :

def funct( tuples ):
	rval = []
	for a,b in tuples:
		rval.append( ( a, b*9 ))
	return tuple(rval)

or :

tuple( [ (a,b*9) for a,b in tuples ] )




More information about the Python-list mailing list