Multiple lists


hello, here is my code. It works, but it prints list a instead of list b which is the largest one. I don't know why. Please help mee...

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
print b

Replace this line with your code.


Hahaha, since I solved this exercise so quickly, I didn't notice it as well! :slight_smile:

Thanks for putting up the question.

Anyway, zip combines the list's elements in list_a and list_b, and it will stop once it reaches the end of one of the lists (in this case, list_a is only 5 elements) So see the answer in the monitor is correct. because most of the elements are bigger in list_a.




Ideally you should be checking the length of both list in the input before you compare the elements. If you really want to compare lists which are of different length, you can try for reference.


So why does it print 30 and not 19? We got no 30 in list a...


You're still misunderstanding what the code is supposed to do.

Let me paint a picture for you.

element of list_a compared with element of list_b >>> xxx

3 compared with 2 >>> 3 from list_a is bigger, print 3
9 compared with 4 >>> 9 from list_a is bigger, print 9
17 compared with 8 >>> 17 from list_a is bigger, print 17
15 compared with 10 >>> 15 from list_a is bigger, print 15
19 compared with 30 >>> 30 from list_b is bigger, print 30

Do you get it now? We're not checking and comparing how long the list are, but which element from each list is higher?


I do understand it now, thanks a lot! :slight_smile:


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