How do I compare the variables when using zip in a for loop?

Question

How do I compare the variables when using zip in a for loop?

Answer

a and b are elements from the resulting combined list. If we have two lists, list_a and list_b, then zip() combines them to the shortest length of the two, like this:

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

The resulting pairs of elements are stored in a and b respectively. So the first time our loop runs, a will be 3 and b will be 2. Comparing these two variables is done as a comparison between two numbers, since they are not lists.
if a > b:, for example.

2 Likes

How do you print the larger of the two lists?

define larger? The list with most elements? Or do you want to compare values at the same index position in the different lists? So compare 3 with 2 (index 1), compare 9 with 4 (index 2) and so on?

1 Like

Yes, I meant the latter.

just use a for loop with zip, and inside the loop compare the values.

2 posts were split to a new topic: Problem creating my own

I’ve used a for loop, the code works but it prints it 4 times, i think
my code is a bit off, can someone help me out:

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

Because you’re printing the whole list list_a every time.
You only want to print a if a is bigger, and b if b is bigger

1 Like

how do i print the larger(list with the most elements) list of the two lists

you could compare the length of the lists?

1 Like

can we do it without using len(list) function?

could we? Yea, very likely. But the better question: should you?

We should note that when two lists are not the same length, the zipped list length will be that of the shorter of the two.

zip ([1,2,3,4,5],['a','b','c'])

becomes,

[(1, 'a'), (2, 'b'), (3, 'c')]

and we lose the last two values of the numbers list.

1 Like

why would any one want to loose values of a list?

Why, indeed. One thinks less that someone would want to lose values, and more of the nature of the zip() function to map corresponding values together. The lost values are a consequence and if we are not sure that the lists are of the same length then we should not be using a function that expects them to be equal in length.

1 Like

Both the ways it is working , Python is a great language to learn

9 Likes

Why not reduce your last two lines of code to simply:
print max(a,b)

?

1 Like

So I understand how the function works, band that it runs through each index of multiple lists at once and does a direct comparison, I’ve noticed a consequence of this is that it doesn’t print the numbers in ascending order, so in this example 15 comes after 17, is there a way of sorting the list so it is printed in ascending order? I understand there’s quite a few reasons why you might not want to do that ie if you just wanted to look at sales of specific product across multiple stores, in date order and only view the figures from the highest store and in April you sold 17 and in May you sold 15.

1 Like

When given two lists which we’re told are corresponding, sorting them is out of the question, at least until we’ve combined them into a single data structure.

From there we can sort by column 1 or by column 2. zip() works with multiple lists but the concept is the same. Select the column to sort by, and perform the sort.

sorted() is ideal for this as it makes a copy and leaves the original data untouched.

>>> a = [3, 8, 17, 15, 19, 12, 7, 4, 11, 9, 5]
>>> b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]
>>> c = sorted(zip(a, b), key=lambda x: x[0])
>>> c
[(3, 2), (4, 60), (5, 90), (7, 50), (8, 4), (9, 80), (11, 70), (12, 40), (15, 10), (17, 8), (19, 30)]
>>> 

Notice that sorted() is able to work directly on the zip object but still returns a list? a and b are unchanged and the zip object got consumed in the process.

2 Likes