Programming Examples

Python program to compare two equal sized list and print the first index where they differ


Write a python program to compare two equal sized list and print the first index where they differ

Solution

list1=eval(input("Enter list 1 : "))
list2=eval(input("Enter list 2 : "))
index=-1
if(len(list1)!=len(list2)):
    print("Length of both list are not same")
else:
    for a in range(0,len(list1)):
        if list1[a]!=list2[a]:
            index=a
            break
    if index>=0:
        print("the elements are differ and the first index where it differ is :",index)
    else:
        print("The list elements are same")
Output

Enter list 1 : [1,2,3,4]

Enter list 2 : [1,2,3]

Length of both list are not same

>>> 

======================== 

Enter list 1 : [1,2,3,4,5]

Enter list 2 : [1,2,5,6,7]

the elements are differ and the first index where it differ is : 2

>>> 

======================== 

Enter list 1 : [1,2,3]

Enter list 2 : [1,2,3]

The list elements are same

>>> 

CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online Exam Quiz O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Best Website and Software Company in Allahabad Website development Company in Allahabad