I have two lists which contain titles and links to interesting pages (trying to write a scraper).

Where I'm falling down is on the actual printing.

for i in var1:
    print i

for a in var2:
    print a

Prints all of list 1 and then all of list 2. If I try this

for i in var1:
    print i
    for a in var2:
        print a

I get one of var1 printed, then 10 of var2 this continues till end of list.

How can I get python to print 1 line from list 1, 1 line from list 2 and then continue till the lists are finished?

THank you in advance.

Recommended Answers

All 2 Replies

Use zip

for i, a in zip(var1, var2):
    print i
    print a

Items are printed until the shortest list is consumed.

Excellent - thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.