0

I'm a bit puzzled with converting following lambda function into a regular function:

my_fun = lambda x,y:[i(j) for i,j in zip(x,y)]

where x is a list of types, say

types = [float,float,float]

and where y is a list of integer values, say:

values = [1,2,3]

so, basically my_fun() converts integers into floats. When I run it this way:

new_values = my_fun(types,values)

it returns a new list with floats.

Now when it comes to define a function in a regular way:

def my_fun(x,y):
    for i,j in zip(x,y):
        i(j)

it stops working. I'm not putting return inside / outside of a loop as it will obviously return first / last instance once it hits it and not trying to assign a new list like:

def my_fun(x,y):
    new_vals = []
    for i,j in zip(x,y):
       new_vals.append(i(j))
    return new_vals

because in this case this function looks to overwhelmed to me. Can someone please explain fundamental difference between lambda and regular function in my case, as it seems I am missing some simple basic knowledge about Python 3.6?

I thought it might have been for list-comprehension I'm using in my lambda but I couldn't explain it to myself. Many thanks!

Vlad
  • 23
  • 5
  • "*I'm not putting return inside / outside of a loop as it will obviously return first / last instance once it hits it*" <= This is not Perl. Without explicit return, your function returns `None`. There's no implicit "return value of last expression". – dhke Apr 07 '17 at 06:12
  • @dhke I was just thinking how to make this function make a multiple returns to produce a list of converted values but obviously my knowledge was not enough to make any sense of it. – Vlad Apr 07 '17 at 06:35

2 Answers2

3

This seems more a misunderstanding about list comprehensions. You could express this as:

def my_fun(x,y):
    return [i(j) for i,j in zip(x,y)]

The problem in your first example is you don't return anything. You could use yield to give you an iterator.

def my_fun(x,y):
    for i,j in zip(x,y):
        yield i(j)
Community
  • 1
  • 1
CodeMonkey
  • 4,067
  • 1
  • 31
  • 43
  • does list-comprehension has some different features oppose to a simple for-loop? I mean it seems a bit veiled to me when I make a list-comprehension and pass it to an undeclared variable, the variable gets populated with a new list of values, e.g. `variable = [i(j) for i,j in zip(types,vals)]` – Vlad Apr 07 '17 at 06:42
  • 1
    I don't quite understand what you mean by veiled. Python assigns the newly created list to `variable`, in the background it still has to iterate through the list and create it. – CodeMonkey Apr 07 '17 at 07:05
1

equivalent of this lambda

lambda x,y:[i(j) for i,j in zip(x,y)] #returns an array/list

is,

def myfun(x,y):
    return [i(j) for i, j in zip(x,y)]

or your last snippet.

below function

def my_fun(x,y):
    for i,j in zip(x,y):
        i(j)

does not return anything. it just passes j argument to i function and calls it. there is neither a declaration for an array, nor a set of procedures to store results on that array.

marmeladze
  • 6,468
  • 3
  • 24
  • 45