0

I would like to merge two lists:

lon: [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]

lat: [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]

to get:

coordinates: [[14.21055347, 48.22824817], [14.21055347, 48.22824817], [16.39356558, 48.18617251], [16.39356558, 48.18617251], [14.21055347, 47.65823679]]

How is this done efficiently for very long lists of lon/lat?

Thanks for your help!

Antoine Delia
  • 1,728
  • 5
  • 26
  • 42
graukas
  • 41
  • 7

5 Answers5

3

You'll want to use the zip() function for that. Like most of python3 built-ins it uses iterators so is quite performant.

>>> lon = [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
>>> lat = [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]
>>> coordinates = zip(lon, lat)
>>> print(list(coordinates))
[(14.21055347, 48.22824817), (14.21055347, 48.22824817), (16.39356558, 48.18617251), (16.39356558, 48.18617251), (14.21055347, 47.65823679)]
Jim Wright
  • 5,905
  • 1
  • 15
  • 34
1

You can take advantage of the map built-in function as well as lambda expressions.

lon = [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
lat = [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]

coordinates = list(map(lambda x, y: [x, y], lon, lat))

print(coordinates)
>>> [[14.21055347, 48.22824817], [14.21055347, 48.22824817], [16.39356558, 48.18617251], [16.39356558, 48.18617251], [14.21055347, 47.65823679]]
Antoine Delia
  • 1,728
  • 5
  • 26
  • 42
  • Python's in-built zip command is more efficient than this. – Vishnudev Krishnadas Feb 02 '21 at 09:48
  • The `zip` function returns an iterator of tuples, which is not exactly what the post wants. – Antoine Delia Feb 02 '21 at 09:51
  • 1
    I know that. I'm just mentioning that a list of tuples is a better data structure for such purposes and `zip` function is an efficient way. There's nothing wrong with your approach. – Vishnudev Krishnadas Feb 02 '21 at 09:55
  • Thank you - that's exactly how I needed it! One more question if I may - is there a way to remove only those dublicates that are successive? BRG – graukas Feb 02 '21 at 10:58
  • You can use [groupby](https://docs.python.org/3/library/itertools.html#itertools.groupby). See [this answer](https://stackoverflow.com/a/5738933/4141606) for some examples. – Antoine Delia Feb 02 '21 at 11:03
0
lon = [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
lat = [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]

coordinates = [[i,j] for i,j in zip(lon, lat)]

print(coordinates)
Deven Ramani
  • 751
  • 4
  • 10
  • Hello and welcome to SO! While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Tomer Shetah Feb 02 '21 at 10:39
-1

I'm not quite sure if I'm understanding your question correctly, but if you want to match the two arrays, just do:

name = [array1, array2]

Mika
  • 1
  • 1
-1

Just use map function

lon = [14.21055347, 14.21055347, 16.39356558, 16.39356558, 14.21055347]
lat = [48.22824817, 48.22824817, 48.18617251, 48.18617251, 47.65823679]
coordinates = list(map(lambda x,y: [x,y], lon,lat))
print(coordinates)

output is :- [[14.21055347, 48.22824817], [14.21055347, 48.22824817], [16.39356558, 48.18617251], [16.39356558, 48.18617251], [14.21055347, 47.65823679]]

Map takes as an input a function so had to introduce the lambda function

Shuvam Paul
  • 120
  • 4