Reputation: 1
I have a list, loc_combinations, with a length of 91806 of unique ID pairs structured as so:
[(1,2), (1,3), 1,4)...(452, 454)]
I am trying to apply the same function distance_calculator
to each pair in the list which returns a single value, the distance. I was able to get my answer using a for loop but was hoping someone could show me how to do it using Lambda and list comprehension.
Here is the for loop:
distance_list = []
for i in range(len(loc_combinations)):
distance_list.append(distance_calculator(id1 = loc_combinations[i][0], id2 = loc_combinations[i][1]))
Upvotes: 0
Views: 1524
Reputation: 733
distance_list = [
distance_calculator(id1=x, id2=y)
for x, y in loc_combinations
]
Upvotes: 0
Reputation: 1824
No need for a lambda function if you have a working function defined. Your code as a list comprehension looks like this:
distance list = [distance_calculator(x[0], x[1]) for x in loc_combinations]
Upvotes: 4