0

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]))

2 Answers 2

4

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]
Sign up to request clarification or add additional context in comments.

1 Comment

or distance_calculator(*x) if you're nasty
0
distance_list = [
    distance_calculator(id1=x, id2=y)
    for x, y in loc_combinations
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.