Why it is not working?


#1

This is easy and does not work/....why?

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


for i in zip(list_a, list_b):
    # Add your code here!
    if list_a[i] >= list_b[i]:
        print list_a[i] 
    else:
         print list_b[i]

receive the error:
Traceback (most recent call last):
File "python", line 7, in
TypeError: list indices must be integers, not tuple


#2

here:

for i in zip(list_a, list_b):

how can you only have iterator for two list? use two:

for i,j in zip(list_a, list_b):
    print i,j

i will hold the items of list_a, j the items of list_b


#3

thanks!
but... I need the i=j all the time, because I need it in the same place...
so, it seem to be waste of parameter.

even due , the zip function should enter the " for " only for the shorter of the two lists.
and I get an error that it out of range.
how come?
"
IndexError: list index out of range
"

and why it is nt priniting all the elements in the list?


#4

no? even if you look in the official documentation you will see zip have two parameters, if you ran the code i provided, you would see that i and j have the numbers from the same index of the different lists. So not a waste of a parameter


#5

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