Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the values of a list that is inside another list as arguments of a function in Python

I am very new to Python. I have a function DISTANCE(lat1, long1, lat2, long2) that calculates the distance between 2 points.

Then I have a list called POINTS, where each value is another list which contains those four values.

I would like to obtain the sum of the results of the function DISTANCE for all the values inside POINTS.

Can anybody help me with that? Thanks!

like image 993
andresmechali Avatar asked Nov 30 '25 05:11

andresmechali


1 Answers

sum(DISTANCE(*p) for p in POINTS)

The * here is the syntax for Unpacking Argument Lists, also called the splat operator. This passes the contents of an iterable as the positional arguments to a function, so if p were [1, 2, 3, 4], DISTANCE(*p) would be the same as DISTANCE(1, 2, 3, 4).

like image 144
Andrew Clark Avatar answered Dec 02 '25 17:12

Andrew Clark