Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Python: Reverse every 2 items on a list

I am working on a Django platform where i am supposed to submit a list of polygon coordinates from google maps to search through a dataset. So far, i have managed to collect the coordinates from the frontend but they are mixed up i.e the longitudes start then latitude follows and ideally ,the longitudes are supposed to start then the latitudes to follow

Here is a working sample of the list i have: coordinates = [65.56147597726766, -104.83033675930798, 64.44751706485646, -93.93189925930798, 58.232434372977615, -98.85377425930798]

The desired format should be: coordinates = [-104.83033675930798,65.56147597726766, -93.93189925930798,64.44751706485646, -98.85377425930798,58.232434372977615]

Any help will be highly appreciated.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can work with list comprehension where we use slices of the original list:

[x for i in range(0, len(coordinates), 2) for x in reversed(coordinates[i:i+2])]

for the given sample data, we get:

>>> coordinates = [65.56147597726766, -104.83033675930798, 64.44751706485646, -93.93189925930798, 58.232434372977615, -98.85377425930798]
>>> [x for i in range(0, len(coordinates), 2) for x in reversed(coordinates[i:i+2])]
[-104.83033675930798, 65.56147597726766, -93.93189925930798, 64.44751706485646, -98.85377425930798, 58.232434372977615]
over 4 years ago · Santiago Trujillo Report

0

You could do:

coordinates = [65.56147597726766, -104.83033675930798, 64.44751706485646, -93.93189925930798, 58.232434372977615, -98.85377425930798]

res = [val for coo in zip(coordinates[1::2], coordinates[::2]) for val in coo]
print(res)

Output

[-104.83033675930798, 65.56147597726766, -93.93189925930798, 64.44751706485646, -98.85377425930798, 58.232434372977615]
over 4 years ago · Santiago Trujillo Report

0

Another way:

coordinates = [65.56147597726766, -104.83033675930798, 64.44751706485646, -93.93189925930798, 58.232434372977615, -98.85377425930798]


result = coordinates[1:] + [0]
result[1::2] = coordinates[0::2]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!