ABC
ABC

Reputation: 55

Defining list from a function with multiple arguments

I have the following function:

def func(x, y, z):
  d = -5.9
  T = 230.0

  f = x - y * T + z + d

  return f

From this, I want to make a single list of f-values extracted from func(x, y, z). The values x, y, and z are three different lists of values.

My question is, how do I make the list of f-values? I have tried the following, but it did not work properly:

f_list = [func(x, y, z) for x in x_list for y in y_list for z in z_list]

Upvotes: 1

Views: 48

Answers (1)

Netwave
Netwave

Reputation: 42796

I think you want to use zip:

f_list = [func(*vals) for vals in zip(x_list, y_list, z_list)]

or for clearance:

f_list = [func(x, y, z) for x, y, z in zip(x_list, y_list, z_list)]

You can also use itertools.starmap:

import itertools
f_list  = list(itertools.starmap(func, zip(x_list, y_list, z_list)))

Upvotes: 3

Related Questions