Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two lists element-wise in python [duplicate]

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.

like image 731
ARINDOM K Avatar asked May 04 '19 19:05

ARINDOM K


People also ask

How do I compare two lists for duplicates in Python?

Python collection.counter() method can be used to compare lists efficiently. The counter() function counts the frequency of the items in a list and stores the data as a dictionary in the format <value>:<frequency>. If two lists have the exact same dictionary output, we can infer that the lists are the same.

How do I compare the contents of two lists in Python?

sort() and == operator. The list. sort() method sorts the two lists and the == operator compares the two lists item by item which means they have equal data items at equal positions. This checks if the list contains equal data item values but it does not take into account the order of elements in the list.

How do I find identical elements in two lists in Python?

Using the intersection() Function This is the easiest method to find common elements in two lists in Python. As the name suggests, the intersection() function is a built-in python function that is used to return a set that contains the elements which are common in two sets.

How do I compare two identical lists?

Using Counter() , we usually are able to get frequency of each element in list, checking for it, for both the list, we can check if two lists are identical or not. But this method also ignores the ordering of the elements in the list and only takes into account the frequency of elements.


1 Answers

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")
like image 58
glhr Avatar answered Sep 24 '22 07:09

glhr