Simon Breton Simon Breton - 1 month ago 5
Python Question

Build a single list of element from bi-dimensional array list

I'm totally noob to python so please forgive my mistake and lack of vocabulary. Long Story Short, I have the following list of array :

[url1, data1][url2, data2][url3, data3]etc...


I want to build a simple list of element by only keeping the url. So I'm doing this :

if results:
for row in results.get('rows'):
data.append(row[:1])
print data


for this result:

[[url1][url2][url3]etc...]


However I would like to have something like this :

[[url1, url2, url3,etc...]


I've imported numpy and tried this but doesn't work :

np.array(data).tolist()


Any help ?

thanks.

Answer

If you just want the url, and your data is basically a list of lists then you can just use the index number, in this case [0] as url is the 1st element in a nested list

l = [['url1', 'data1'],['url2', 'data2'],['url3', 'data3']]
endlist = []
for i in l:
    endlist.append(i[0])

print endlist

Output:

['url1', 'url2', 'url3']

However, make sure how your data is structured. A list of lists looks like [[],[],[]], where each nested list is seperated by a , (comma) which is absent in the example you posted.

Comments