10

I have two list

first= (1,2,3,4,5,6)
last=(6,5,4,3,2,1)

I need to compare the corresponding values only. I have used the below code and getting 36 results as the 1st element in first is comparing with all the six elements of last list.

for x in first:
    for y in last:
        if x>y:
            print("first is greater then L2",y)
        elif x==y:
            print("equal")
        else:
            print("first is less then L2",y)

irst= (1,2,3,4,5,6)
last=(6,5,4,3,2,1)
for x in first:
    for y in last:
        if x>y:
            print("first is greater then L2",y)
        elif x==y:
            print("equal")
        else:
            print("first is less then L2",y)

output:

L1 is less then L2 6
L1 is less then L2 5
L1 is less then L2 4
L1 is less then L2 3
L1 is less then L2 2
go dada
L1 is less then L2 6
L1 is less then L2 5
L1 is less then L2 4
L1 is less then L2 3
go dada
L1 is greater then L2 1
L1 is less then L2 6
L1 is less then L2 5
L1 is less then L2 4
go dada
L1 is greater then L2 2
L1 is greater then L2 1
L1 is less then L2 6
L1 is less then L2 5
go dada
L1 is greater then L2 3
L1 is greater then L2 2
L1 is greater then L2 1
L1 is less then L2 6
go dada
L1 is greater then L2 4
L1 is greater then L2 3
L1 is greater then L2 2
L1 is greater then L2 1
go dada
L1 is greater then L2 5
L1 is greater then L2 4
L1 is greater then L2 3
L1 is greater then L2 2
L1 is greater then L2 1
y

I need results by comparing the corresponding elements only. Which means there should be only six outputs.

glhr
  • 4,439
  • 1
  • 15
  • 26
ARINDOM K
  • 135
  • 1
  • 2
  • 7

2 Answers2

12

first and last are tuples, not lists (lists elements are within square brackets like [1,2,3]).

You can use zip(first,last) to create a list of pairs from the two tuples:

[(1, 6), (2, 5), (3, 4), (4, 3), (5, 2), (6, 1)]

then iterate over the tuples and compare each pair:

first = (1,2,3,4,5,6)
last = (6,5,4,3,2,1)

for l1,l2 in zip(first,last):
    if l1 < l2:
        print("l1 < l2")
    elif l1 > l2:
        print("l2 > l1")
    elif l1 == l2:
        print("l1 == l2")

Output:

l1 < l2
l1 < l2
l1 < l2
l2 > l1
l2 > l1
l2 > l1

Another approach would be to iterate over the indices, however this approach is less Pythonic:

for i in range(len(first)):
    l1 = first[i]
    l2 = last[i]
    if l1 < l2:
        print("l1 < l2")
    elif l1 > l2:
        print("l2 > l1")
    elif l1 == l2:
        print("l1 == l2")
glhr
  • 4,439
  • 1
  • 15
  • 26
  • If you want to know if they are all equal all( [l[0] == l[1] for l in list(zip(a,b))] ) == True. Explanation: inside to out. zip together, then turn the zip into a list of tuples. Compare them using list comprehension. Finally the all checks that they are ALL equal – brian_ds Mar 12 '21 at 22:31
  • if I have a case where the lists are of different length, how do i cycle through the iteration by going back to the first element of the short list to match the next element of the long list? – Shanely Jul 23 '21 at 06:04
3

You should combine the two tuples into a list of two-element, pairwise tuples with zip:

for x, y in zip(first, last):
    if x < y: ... # Your code
DYZ
  • 55,249
  • 10
  • 64
  • 93