I want to iterate through a list of coordinates and see it the last coordinate matches the first coordinate. The coordinates are stored in have a GeoJSON string:


I first use findall method with regex to filter the coordinates from this string. This results in a list with this structure: listx = ['76.863853437826,2.49314161017537', '-76.863723769784,2.4932265188545', '-76.8627395667136,2.49319550581276', '-76.863853431111,2.4931416101711111']

Now, the 'strange' behaviour is that when I use this list in a for loop (for x in list) with an index, it only gives me the first number... Example:

for i in listx:
    first_coord = i[0]
    last_coord = i[-1]

This results in '7' for the first coordinate. I would expect that it should plot the entire coordinate on index 0 (so: '76.863853437826,2.49314161017537').

I am a Phy-beginner, so probably I don't see something important here. Any solution how I can make sure that the entire coordinate string is being selected (first and last coordinate)?

  • 2
    Don't use regular expressions. Use json.
    – user2856
    Jun 13 at 8:26
  • Do you mean you are a "Py-beginner"? I agree that you want to use json.loads() to convert the string to dictionaries/arrays which are trivially iterated. In the future you can pretty-priint the JSON so it's legible in a code block.
    – Vince
    Jun 13 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.