samedi 4 décembre 2021

How to Check if elements of two lists are equal or not using IF statement in python?

I have a multidimensional list. I want to check if the elements of different lists are equal or not using if statement only. I have written different versions using for loop and while loop. I am not getting how to do the same thing using if statement only.

Below are my codes.

A =      [[[1], [2]],
          [[3], [4]],
          [[5], [6]],
          [[7], [8]],
          [[7], [8]]]  

# Checking if two lists are equal or not using for loop
for i in range(len(A)-1):
    if A[i] == A[i+1]:
        print('iteration',[i],'=',A[i],'and','iteration',[i+1],'=',A[i+1],'are equal')

Output

iteration [3] = [[7], [8]] and iteration [4] = [[7], [8]] are equal

Using while loop

# # Checking if two lists are equal or not using while loop
i = 1
while i in range(0, len(A)-1):
    if A[i] == A[i+1]:
        print('iteration',[i],'=',A[i],'and','iteration',[i+1],'=',A[i+1],'are equal')
    
    i = i+1

Output

iteration [3] = [[7], [8]] and iteration [4] = [[7], [8]] are equal

How can I achieve the same result using if statement only?

Aucun commentaire:

Enregistrer un commentaire