Multiple Lists


#1

Ok guys, I'm getting the green light to proceed to the next exercise, but my code is not following the instructions. Here is 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) :
print a,
else:
print b

It gives me the following:
3 9 17 15 30
None

Why is it not displaying list_b? What are the values that are displayed? If possible, a thorough explanation would be greatly appreciated. Thank you.


#2

the simplest explanation would be:

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 a, b

now you can see that a contanis values from list_a and b contains values from list_b (from the same index)

if you are then start comparing, the biggest number of each index in lists get printed

3 is bigger then 2
9 is bigger then 4
17 is bigger then 8
15 is bigger then 10
19 is smaller then 30


#3

The instructions are as follows:
Compare each pair of elements and print the larger of the two.
So why isn't list_b printing in its entirety?


#4

Never mind, I can't read apparently.

It was asking me to compare elements in the list and print the numerically largest element, not print out the longer list.

Thanks for helping this illiterate soul.


#5

because we are comparing values from the list, we don't check which list is the biggest.

first we compare both values from index 0 of both lists, list_a has value of 3 at index 0, and list_b has a value 2 at index 0, so 3 is bigger then 3, so its printed

a and b contains values from the list. That is what we are comparing

zip allows us to loop to get us values from both lists simultaneously


#6

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