how to compare an element of a list with an external value | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to compare an element of a list with an external value

18th Mar 2020, 9:46 AM
Captian A
2 Answers
+ 3
You can compare like this words = ["hello", "world", "spam", "eggs"] index = 0 while index < len(words): if words[index] == "spam": print(index) #2 index = index + 1
18th Mar 2020, 10:04 AM
A͢J
A͢J - avatar
0
First step is to get the element. The positions or index in list starts from 0. So, for example letters=['A' , 'B' , 'C' , 'D'] print(letters[0])#Outputs A Then you can use them to compare normally like you would do to numbers. Here I have used alphabets to make it easier to understand
18th Mar 2020, 10:19 AM
Taranjeet
Taranjeet - avatar