Steven Matthews
Steven Matthews

Reputation: 11275

Iterating over multiple lists in Python

I have a list within a list, and I am trying to iterate through one list, and then in the inner list I want to search for a value, and if this value is present, place that list in a variable.

Here's what I have, which doesn't seem to be doing the job:

for z, g in range(len(tablerows), len(andrewlist)):
    tablerowslist = tablerows[z]
    if "Andrew Alexander" in tablerowslist:
        andrewlist[g] = tablerowslist

Any ideas?

This is the list structure:

[['Kyle Bazzy', 'FUP dropbox message', '8/18/2011', 'Swing Trade Stocks</a>', '&nbsp;', 'Affiliate blog'], ['Kyle Bazzy', 'FUP dropbox message', '8/18/2011', 'Swing Trade Software</a>', '&nbsp;', 'FUP from dropbox message. Affiliate blog'], ['Kyle Bazzy', 'FUP dropbox message', '8/18/2011', 'Start Day Trading (Blog)</a>', '&nbsp;', 'FUP from dropbox message'], ['Kyle Bazzy', 'Call, be VERY NICE', '8/18/2011', '&nbsp;', 'r24867</a>', 'We have been very nice to him, but he wants to cancel, we need to keep being nice and seeing what is wrong now.'], ['Jason Raznick', 'Reach out', '8/18/2011', 'Lexis Nexis</a>', '&nbsp;', '-'], ['Andrew Alexander', 'Check on account in one week', '8/18/2011', '&nbsp;', 'r46876</a>', '-'], ['Andrew Alexander', 'Cancel him from 5 dollar feed', '8/18/2011', '&nbsp;', 'r37693</a>', '-'], ['Aaron Wise', 'FUP with contract', '8/18/2011', 'YouTradeFX</a>', '&nbsp;', "Zisa is on vacation...FUP next week and then try again if she's still gone."], ['Aaron Wise', 'Email--JASON', '8/18/2011', 'Lexis Nexis</a>', '&nbsp;', 'email by today'], ['Sarah Knapp', '3rd FUP', '8/18/2011', 'Steven L. Pomeranz</a>', '&nbsp;', '-'], ['Sarah Knapp', 'Are we really interested in partnering?', '8/18/2011', 'Reverse Spins</a>', '&nbsp;', "V. political, doesn't seem like high quality content. Do we really want a partnership?"], ['Sarah Knapp', '2nd follow up', '8/18/2011', 'Business World</a>', '&nbsp;', '-'], ['Sarah Knapp', 'Determine whether we are actually interested in partnership', '8/18/2011', 'Fayrouz In Dallas</a>', '&nbsp;', "Hasn't updated since September 2010."], ['Sarah Knapp', 'See email exchange w/Autumn; what should happen', '8/18/2011', 'Graham and Doddsville</a>', '&nbsp;', "Wasn't sure if we could partner bc of regulations, but could do something meant simply to increase traffic both ways."], ['Sarah Knapp', '3rd follow up', '8/18/2011', 'Fund Action</a>', '&nbsp;', '-']]

For any value that has a particular value in it, say, Andrew Alexander, I want to make a separate list of these.

For example:

[['Andrew Alexander', 'Check on account in one week', '8/18/2011', '&nbsp;', 'r46876</a>', '-'], ['Andrew Alexander', 'Cancel him from 5 dollar feed', '8/18/2011', '&nbsp;', 'r37693</a>', '-']]

Upvotes: 0

Views: 1047

Answers (3)

utdemir
utdemir

Reputation: 27216

>>> #I have a list within a list,
>>> lol = [[1, 2, 42, 3], [4, 5, 6], [7, 42, 8]]
>>> found = []
>>> #iterate through one list,
>>> for i in lol:
...     #in the inner list I want to search for a value
...     if 42 in i:
...         #if this value is present, place that list in a variable    
...         found.append(i)
... 
>>> found
[[1, 2, 42, 3], [7, 42, 8]]

Upvotes: 1

michel-slm
michel-slm

Reputation: 9756

Assuming you have a list whose elements are lists, this is what I'd do:

andrewlist = [row for row in tablerows if "Andrew Alexander" in row]

Upvotes: 4

Karl Knechtel
Karl Knechtel

Reputation: 61479

for z, g in range(len(tablerows), len(andrewlist)):

This means "make a list of the numbers which are between the length of tablerows and the length of andrewlist, and then look at each of those numbers in turn, and treat those numbers as a list of two values, and assign the two values to z and g each time through the loop".

A number cannot be treated as a list of two values, so this fails.

You need to be much, much clearer about what you are doing. Show an example of the contents of tablerows before the loop, and the contents of andrewlist before the loop, and what it should look like afterwards. Your description is muddled: I can only guess that when you say "and then I want to iterate through one list" you mean one of the lists in your list-of-lists; but I can't tell whether you want one specific one, or each one in turn. And then when you next say "and then in the inner list I want to...", I have no idea what you're referring to.

Upvotes: 1

Related Questions