0
how to compare an element of a list with an external value
2 Réponses
+ 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
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