gdogg371
gdogg371

Reputation: 4122

Print contents of two lists so corresponding records from each print sequentially

I have two Python lists that contain year increments, staggered by one year (one runs 1999-2013, the other 2000-2014). I want to print these so that the corresponding items within both lists are printed one after another. Here is my code:

list1 = [1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013]
list2 = [2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014]


for x in list1 and y in list2:
    print "list1 - " + str(x)
    print "list2 - " + str(y)

Which is producing the following error:

Traceback (most recent call last):
  File "C:\Python27\newtets\newtets\spiders\test3.py", line 12, in <module>
    for x in list1 and y in list2:
NameError: name 'y' is not defined

I've tried various spins on this (nesting for statements in different combinations, but they all either don't work or dont produce the output in the format that I want, which would be):

list1 - 1999
list2 - 2000
list1 - 2000
list2 - 2001
list1 - 2001
list2 - 2002
...

I think that I am almost there, but can't quite get it.

Can anyone help?

Thanks

Upvotes: 0

Views: 78

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180411

If both lists are the same length you can use enumerate also:

for ind, ele in enumerate(list1): # ind is the index of each element in the list
    print "list1 {}\nlist2 {}".format(ele,list2[ind])
list1 1999
list2 2000
list1 2000
list2 2001
list1 2001
list2 2002

If you have different size lists you might want to use itertools.izip_longest where missing values are filled-in with fillvalue.

list1 = [1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013]
list2 = [2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012]

from itertools import izip_longest

for ele1,ele2 in izip_longest(list1,list2,fillvalue="No corresponding data"):
    print "list1 {}\nlist2 {}".format(ele1,ele2)


list1 1999
list2 2000
list1 2000
list2 2001
list1 2001
list2 2002
list1 2002
list2 2003
list1 2003
list2 2004
list1 2004
list2 2005
list1 2005
list2 2006
list1 No corresponding data
list2 2007
list1 No corresponding data
list2 2008
list1 No corresponding data
list2 2009

Upvotes: 1

TheSoundDefense
TheSoundDefense

Reputation: 6935

What you want is the zip function.

for x, y in zip(list, list2):
  print "list1 - " + str(x)
  print "list2 - " + str(y)

The zip function takes two lists and interleaves them into a list of tuples, like so:

>>> list1 = [1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013]
>>> list2 = [2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014]
>>> zip(list1,list2)
[(1999, 2000), (2000, 2001), (2001, 2002), (2002, 2003), (2003, 2004), (2004, 2005), (2005, 2006), (2006, 2007), (2007, 2008), (2008, 2009), (2009, 2010), (2010, 2011), (2011, 2012), (2012, 2013), (2013, 2014)]

Upvotes: 1

Related Questions