Multiple lists


#1



Step up your FOR's, multiple lists

the instructions to the exercise are the following: "Compare each pair of elements and print the larger of the two."


I am working on stepping up your FOR's unit, doing the multiple list exercise (#16) (sorry I cannot get the link to work properly when I past it here), as it demonstrate what the ZIP function does. I tinkered around with the exercise, and the code below worked, but I am wondering if there is anything that I am missing as I feel the code is not very elegant... Anyone with a comment for my code?


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

Loops prob16 multiple lists
#2

You do not need to perform this test ...

if a>=b or b>=a:

#3

I do think that the if statement is clunky... but in the hint is what they suggest to use. (Thanks for your suggestion, I did run the code without the if statement and it worked just fine. I guess my instinct was right in that the if statement is a bit off)

HINT
a is an element from list_a and b is an element of list_b. Use an if statement to compare the two and print whichever is larger.


#4

the exercise suggest you use an if and else statement to check which number is bigger.

this is possible, if you write the condition right.

but you decided to use max() then you don't need the if/else, since max will give you the highest value


#5

@systemsurfer02624 ,

You could follow the hint by using this condition ...

if a > b:

Within the if block you would display the value of a. That would be followed by an else block, in which you would display the value of b. By taking this approach you could eliminate the call to the max function.


#6

Thanks! that is the hint i was looking for... so this is another way of doing this exercise:

for a, b in zip(list_a, list_b):
----if a > b:
-------print a
----else:
-------print b


#7

Thanks for your feedback. You just confirmed what I was suspecting but didn't know if I was right, with the MAX function, the if statement was totally redundant....


#8

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