How can I look up the index of an item in my list?

In below mentioned code I can get the number by using index value

heights = [61, 70, 67, 64, 65]


I am wondering what if i want to use number in order to find index. For example i want to find at what index is number 64 is?

Can we do this in python?

Hi @prateekjain2508,

Try this:

i = heights.index(64)

… or write your own function:

def find_all(my_lst,  n):
    for i in range(len(my_lst)):
        if my_lst[i] == n:
            # do something with i

(Obviously, you could make a list of i’s, print them, or do anything you like with them.)
This also shows the cool tie-in between the range() function, the final list index, and list length.


The use of index is very helpful here. However, if there was another 64 in the list, this method would not capture the index of that second 64. Actually, I tried to do so but the printed result showed only the index of the 64 which was firstly in the list. Is this a drawback of “index”? Maybe the solution here is one like that described by @patrickd314 in the same forum.

1 Like

Yes, .index() has that drawback, but it also permits a start argument that would let us store the first index, then set the start to 1 past that, and search again, store that index, and repeat. See what you can come up with using this approach.

There is another object that can make it easier for us to find all the indices of matching items…enumerate. We find this in the module on iterators, so I won’t try to jump the queue here.