change list of tuples to 2 lists - Python
<p>I have a list of tuples (in this case its coordinates for latitude and longitude)</p>

<pre><code>[(51.69768233153901, -5.039923897568534),
(52.14847612092221, 0.33689512047881015),
(52.14847612092221, 0.33689512047881015),
....]
</code></pre>

<p>I am trying to get them into 2 separate lists (one for latitude and one for longitude)</p>

<p>I cannot work out how to loop through to add them to the lists, so far I have: </p>

<pre><code>lat = latlon_df.at[0,'LatLon'][0] 
lon = latlon_df.at[0,'LatLon'][1]
</code></pre>

<p>which identifies the first of each. Could someone show me how do create the 2 new lists?</p>

<p>Thanks for your help!</p>

<p>If I make a list of tuples with a list comprehension like:</p>

<pre><code>In [131]: ll = [(i,j) for i,j in zip(range(10),range(5,15))]
In [132]: ll
Out[132]: 
[(0, 5),
 (1, 6),
 (2, 7),
 (3, 8),
 (4, 9),
 (5, 10),
 (6, 11),
 (7, 12),
 (8, 13),
 (9, 14)]
</code></pre>

<p>I can easily split it into 2 list with 2 list comprehensions:</p>

<pre><code>In [133]: [i[0] for i in ll], [i[1] for i in ll]
Out[133]: ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
</code></pre>

<p>Yes, it involves looping over <code>ll</code> twice, but I'd end up looping over it many more times if tried to work out a way of doing it just one loop.  Sometimes the simple straightforward approach is better.</p>

<p>Having said that, there is a list version of <code>transpose</code> that does the job</p>

<pre><code>In [134]: list(zip(*ll))
Out[134]: [(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (5, 6, 7, 8, 9, 10, 11, 12, 13, 14)]
</code></pre>

<p>This gives a list of 2 tuples, each with a 'column' of the original.  My original constructor can be simplified to</p>

<pre><code>list(zip(range(10),range(5,15)))
</code></pre>

<p>So the <code>list(zip(*...</code> flips the list back and forth.</p>

<p>But: </p>

<pre><code>lat = latlon_df.at[0,'LatLon'][0]
</code></pre>

<p>suggests that the list of tuples might actually be a <code>pandas</code> dataframe.  <code>pandas</code> probably has its own tools for indexing in this way.  You previously asked about this in dataframe terms</p>

<p><a href="http://stackoverflow.com/questions/39422043/removing-round-brackets-from-a-dataframe-of-lat-lon-pairs">Removing round brackets from a dataframe of lat/lon pairs</a></p>

<p>If the list was really a structured array:</p>

<pre><code>In [143]: arr=np.array(ll, dtype='i,i')
</code></pre>

<p>The print display looks a lot like a list of tuples (though missing some ,)</p>

<pre><code>In [144]: print(arr)
[(0, 5) (1, 6) (2, 7) (3, 8) (4, 9) (5, 10) (6, 11) (7, 12) (8, 13) (9, 14)]
In [145]: arr
Out[145]: 
array([(0, 5), (1, 6), (2, 7), (3, 8), (4, 9), (5, 10), (6, 11), (7, 12),
       (8, 13), (9, 14)], 
      dtype=[('f0', '&lt;i4'), ('f1', '&lt;i4')])
</code></pre>

<p>It is easy to select 'fields' by name:</p>

<pre><code>In [146]: arr['f0']
Out[146]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)
In [147]: arr['f1']
Out[147]: array([ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14], dtype=int32)
</code></pre>

python tuples ipython