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

>>> 

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad