How to compare to two element of same list in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to compare to two element of same list in python?

I want to compare two element of same list in python.

2nd Mar 2019, 12:56 PM
zeeshan
zeeshan - avatar
2 Answers
+ 3
list1 = [1,2,3,1] print(list1[0]==list1[1]) #returns false print(list1[0]==list1[3]) #returns true
2nd Mar 2019, 1:16 PM
Nathan Lewis
Nathan Lewis - avatar
+ 1
def compare(lst,idx1,idx2): return lst[idx1] == lst[idx2] # when you don’t know the index, you can use an iterator. (e.g. itertools module)
2nd Mar 2019, 2:54 PM
sumito_hz
sumito_hz - avatar