Multiple list comparison


#1



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

I am getting the below error for the second code


15
Traceback (most recent call last):
File "python", line 5, in
IndexError: list index out of range


**Can someone explain why this code is working -**

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):
    print max(a,b)


**But this is not -** 

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):
    print max(list_a[a],list_b[b])

Whats is it that I have not understood?


#2

a and b are items inside of list_a and list_b.

suppose list_a at index 9 ,
as 9 is item of list_a

when you do this.. it does not exist because there is no items that exist at index 9.
and thus it throws the error.
same thing true for list_b.


#3

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