16. Multiple Lists


#1



https://www.codecademy.com/courses/python-beginner-en-cxMGf/2/3?curriculum_id=4f89dab3d788890003000096#


It works fine. But I wanted to know how the zip function works. And is there any other better way to run it?


list_a = [3, 9, 17, 15, 19]
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]

for a, b in zip(list_a, list_b):
    # Add your code here!
    if a > b:
        print a 
    else:
        print b


#2

The above two lists are zipped by pairing up the first value in one with the first value in the other and storing as a tuple. The second gets paired with the second, the third with the third, and so on. Any jagged values that cannot be paired up, are ignored.

>>> list_a = [3, 9, 17, 15, 19]
>>> list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]
>>> z = zip(list_a,list_b)
>>> z
<zip object at 0x02E9B440>
>>> list(z)
[(3, 2), (9, 4), (17, 8), (15, 10), (19, 30)]
>>>

This is Python 3 code, but the resulting list of tuples is the same in both versions.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.