ChamingaD
ChamingaD

Reputation: 2928

How to iterate through a list of lists in python?

I have a list of lists like this.

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']]

Now i need to iterate through the above list and output a list of strings, as shown below (without the numbers in the original list)

documents = ['Human machine interface for lab abc computer applications',
             'A survey of user opinion of computer system response time',
             'The EPS user interface management system']

Upvotes: 15

Views: 75615

Answers (7)

Vaibhav Singh
Vaibhav Singh

Reputation: 139

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'
]

Upvotes: 1

K Adamu
K Adamu

Reputation: 39

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]

Upvotes: 0

Richard
Richard

Reputation: 1

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

Upvotes: 0

machine yearning
machine yearning

Reputation: 10119

The simplest solution for doing exactly what you specified is:

documents = [sub_list[0] for sub_list in documents]

This is basically equivalent to the iterative version:

temp = []
for sub_list in documents:
    temp.append(sub_list[0])
documents = temp

This is however not really a general way of iterating through a multidimensional list with an arbitrary number of dimensions, since nested list comprehensions / nested for loops can get ugly; however you should be safe doing it for 2 or 3-d lists.

If you do decide to you need to flatten more than 3 dimensions, I'd recommend implementing a recursive traversal function which flattens all non-flat layers.

Upvotes: 35

user109839
user109839

Reputation:

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.

Upvotes: 5

KobeJohn
KobeJohn

Reputation: 7545

**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.

Upvotes: 1

gfortune
gfortune

Reputation: 2609

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]

Upvotes: 8

Related Questions