I have a list of lists like this.
documents = [[\'Human machine interface for lab abc computer applications\',\'4\'],
[\'A survey of user opinio
**edit. thanks DSM. This is wrong as it just flattens the lists. I didn't notice the extra data inside the list after the text that OP wants to ignore.
Ok I'll make it really easy for you!
itertools.chain.from_iterable(documents)
As others have said, it depends on what final behavior your need. So if you need something more complex than that, use recursive traversal or if you are like me, use an iterative traversal. I can help you with that if you need it.
As explained in http://docs.python.org/library/operator.html#operator.itemgetter, You can also try with
from operator import itemgetter
documents = map(itemgetter(0), documents)
that should be faster than using an explicit loop.
If you want to simply iterate over the loop and do things with the elements (rather than the specific results requested in the question), you could use a basic for loop
for row in documents:
#do stuff with the row
print(row)
for column in row:
#do stuff with the columns for a particular row
print(column)
if(row[1] > 10):
print('The value is much too large!!')
This is a language feature known as "flow control".
Note that if you only want the result given in the question, a list comprehension like machine yearning provided is the best way to do it.
documents = [doc[0] for doc in documents]
Note that it discards your original documents list (you are overwriting the original variable) so use the following if you want to have a copy of the first column as well as a copy of your original list:
document_first_row = [doc[0] for doc in documents]
You can also use zip with argument unpacking to transform a list of "rows" into a list of columns:
rows=[[1,'a','foo'],
[2,'b','bar'],
[3,'c','baz']]
columns=zip(*rows)
print columns
#[(1,2,3),
# ('a','b','c'),
# ('foo','bar','baz')]
print columns[0]
#(1,2,3)
the * operator passes all the rows in as seperate arguments to zip
zip(*rows) == zip(row1,row2,row3,...)
zip takes all the rows and assembles columns with one item from each list
you could use numpy array
for instance
document = [['the quick brown fox', '2' ],['jumped over the lazy fox ','3']]
import numpy as np
document = np.array(document)
document=document[:,0]
The question is dead but still knowing one more way doesn't hurt:
documents = [['Human machine interface for lab abc computer applications','4'],
['A survey of user opinion of computer system response time','3'],
['The EPS user interface management system','2']]
document = []
for first,*remaining in documents:
document.append(first)
print(document)
['Human machine interface for lab abc computer applications',
'A survey of user opinion of computer system response time',
'The EPS user interface management system'
]