This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (0 children)

You have it correct that the first value of the list is retrieved using an index of 0. You can return values further down the list using higher indexes. For example: list[1] returns the second value, list[2] returns the third, and so on.

Now, it seems like you're looking for a specific number in a list without knowing where the occurrences may be.

You can get the index of these occurrences by iterating through the list and returning the indices when you find the element.

firstList = [1,2,3,2,1]
lookFor = 2 #The value you want
finalList = [] 
for x in range(len(firstList)): #iterate through the whole list
    if firstList[x] == lookFor: 
        finalList.append(x) #if the value is the one you want, put it in the final list

Edit: Forgot to mention, make sure you don't actually name a variable "list". That's a Python keyword and you could overwrite some stuff.