Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the position of an element in a list , in Python?

Tags:

for s in stocks_list:
    print s

how do I know what "position" s is in? So that I can do stocks_list[4] in the future?

like image 546
TIMEX Avatar asked Nov 19 '09 10:11

TIMEX


People also ask

How do you find the position of an item in a list Python?

To find a position of the particular element you can use the index() method of List class with the element passed as an argument. An index() function returns an integer (position) of the first match of the specified element in the List.

How do you check the position of an item in a list?

Python – Find Index or Position of Element in a List. To find index of the first occurrence of an element in a given Python List, you can use index() method of List class with the element passed as argument. The index() method returns an integer that represents the index of first match of specified element in the List.

How do you find the index of an element in an array in Python?

The index() method returns the index of the given element in the list. If the element is not found, a ValueError exception is raised.

How do you find the position of an element in an array?

To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found. The following illustrates the syntax of the indexOf() method.

How to find the position/index of an element in Python list?

Python find the position/index of an element in List. To find a position of the particular element you can use the index () method of List class with the element passed as an argument. An index () function returns an integer (position) of the first match of the specified element in the List.

How to find index of first occurrence of an element in Python?

To find index of the first occurrence of an element in a given Python List, you can use index () method of List class with the element passed as argument. The index () method returns an integer that represents the index of first match of specified element in the List.

How to find the exact index positions of the given element?

This is how getIndexes () founds the exact index positions of the given element & stores each position in the form of (row, column) tuple. Finally, it returns a list of tuples representing its index positions in the dataframe.

How to add start and end positions of a list in Python?

You can also provide start and end positions of the List, where the search has to happen in the list. Following is the syntax of index () function with start and end positions. start parameter is optional. If you provide a value for start, then end is optional. We shall look into examples, where we go through each of these scenarios in detail.


1 Answers

If you know what you're looking for ahead of time you can use the index method:

>>> stocks_list = ['AAPL', 'MSFT', 'GOOG']
>>> stocks_list.index('MSFT')
1
>>> stocks_list.index('GOOG')
2
like image 174
Ben Dowling Avatar answered Oct 20 '22 15:10

Ben Dowling