I have a function that I want to run several times with a different argument for each successive run. I am not sure how to go about this. I have created a list of the values I want.
> for x in list:
> print(x)
>
> list = ["2", "5", "10"]
>
> for y in list1:
> print(y)
>
> list1 = ["2", "5", "10"]
I want to pass the list to a function that I have.
> Def function(x,y):
z = x*y
> return z
> result = function(x,y)
Could I simply define the arguments as the names of my lists?
I think I am missing another loop.
CodePudding user response:
def function(x, y):
return x*y
Xs = [2, 5, 10]
Ys = [2, 5, 10]
result = [function(x,y) for x, y in zip(Xs, Ys)]
CodePudding user response:
Here's what I think you may be trying to do...
You have two lists. Each list is comprised of strings which represent integer values. You want to multiply corresponding elements from each of the lists and produce an output.
list1 = ['10', '20', '30']
list2 = ['5', '6', '7']
def function(x, y):
return int(x) * int(y)
print(*map(function, list1, list2), sep='\n')
Output:
50
120
210
CodePudding user response:
Def function(x,y):
return x*y
list1 = ["2", "5", "10"]
list2 = ["2", "5", "10"]
for number in range(3):
print(function(list1[number], list2[number]))