{"id":24737,"date":"2021-11-02T09:47:19","date_gmt":"2021-11-02T04:17:19","guid":{"rendered":"https:\/\/python-programs.com\/?p=24737"},"modified":"2021-11-05T17:24:08","modified_gmt":"2021-11-05T11:54:08","slug":"python-list-index-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-list-index-method-with-examples\/","title":{"rendered":"Python List index() Method with Examples"},"content":{"rendered":"

In the previous article, we have discussed Python List extend() Method with Examples<\/a>
\nList in Python:<\/strong><\/p>\n

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.<\/p>\n

List index() Method in Python:<\/strong><\/p>\n

The index() method returns the position in the given list of the given element.<\/p>\n

Syntax:<\/strong><\/p>\n

gvnlist.index(element, start, end)<\/pre>\n

Parameter Values:\u00a0<\/strong><\/p>\n

The list index() method can accept up to three arguments:<\/p>\n

element<\/strong> – the to-be-searched element<\/p>\n

start (optional)<\/strong> – Begin searching from this index<\/p>\n

end (optional)<\/strong> – Search the element all the way up to this index<\/p>\n

Return Value:<\/strong><\/p>\n

The index() method returns the index of the specified list element.
\nA ValueError exception is thrown if the element is not found.<\/p>\n

Note:<\/strong><\/p>\n

Only the first occurrence of the matching element is returned by the index() method.<\/p><\/blockquote>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given List = ['hello', 'this', 'is', 'python', 'programs']\r\nGiven Element = 'this'<\/pre>\n

Output:<\/strong><\/p>\n

The given element { this } in the given list ['hello', 'this', 'is', 'python', 'programs'] is present at the index :\r\n1<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given List = ['hello', 'this', 'is', 'python', 'programs']\r\nGiven Element = 'btechgeeks'<\/pre>\n

Output:<\/strong><\/p>\n

Traceback (most recent call last):\r\n  File \"\/home\/d28185532d674af8eb476a05f17862ba.py\", line 8, in <module>\r\n    resltind = gvnlst.index(gvnele)\r\nValueError: 'btechgeeks' is not in list<\/pre>\n

List index() Method with Examples in Python<\/h2>\n