# python - How to divide list according to index position?

0 votes
1.2k views

### python - How to divide list according to index position?

I have a combined list of `lat`, `lon` and `alt` in a sequence as shown below, now I want to divide the list into 3 parts 1st for `lat`, 2nd for `lon` and third for `alt`.

``````coords = [48.92504247289378, 9.147973368734435, 29707, 48.92504291087322, 9.147998449546572, 29707, 48.9250463088055, 9.148013780873235, 29707, 48.92505484289239, 9.148021595289876, 29707, 48.925070689333246, 9.148024125370592, 29707]

#list format = [lat,lon,alt,lat,lon,alt.....]
``````

excepted output:

``````lat = [48.92504247289378,48.92504291087322,48.9250463088055....]
lon = [9.147973368734435,9.147998449546572, 9.148013780873235...]
alt = [29707,29707,29707,...]
``````

I tried:

Split a python list into other "sublists" i.e smaller lists

Python: Split a list into sub-lists based on index ranges

Split a list into parts based on a set of indexes in Python

And a few other solutions, but none of them worked.

Can anyone help me to get the expected output?

## 1 Answer

0 votes
by (71.8m points)

Just slicing the list would work:

``````coords = [48.92504247289378, 9.147973368734435, 29707, 48.92504291087322, 9.147998449546572, 29707, 48.9250463088055, 9.148013780873235, 29707, 48.92505484289239, 9.148021595289876, 29707, 48.925070689333246, 9.148024125370592, 29707]
lat = coords[::3]
lon = coords[1::3]
alt = coords[2::3]
``````