{"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> 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 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. 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 Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n The element python is present at the third index but not between the indices, resulting in an error.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Covered all examples on python concepts and missed checking out the Python List Method Examples<\/a> then go with the available link & gain more knowledge about List method concepts in python.<\/p>\n In the previous article, we have discussed Python List extend() Method with Examples List in Python: 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 …<\/p>\n
\nList in Python:<\/strong><\/p>\ngvnlist.index(element, start, end)<\/pre>\n
\nA ValueError exception is thrown if the element is not found.<\/p>\nGiven List = ['hello', 'this', 'is', 'python', 'programs']\r\nGiven Element = 'this'<\/pre>\n
The given element { this } in the given list ['hello', 'this', 'is', 'python', 'programs'] is present at the index :\r\n1<\/pre>\n
Given List = ['hello', 'this', 'is', 'python', 'programs']\r\nGiven Element = 'btechgeeks'<\/pre>\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
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
Example-1: If the element is present in the given list<\/h5>\n
\n
# Give the list as the static input and store it in a variable.\r\ngvnlst = ['hello', 'this', 'is', 'python', 'programs']\r\n# Give the element whose index must be located in the given list\r\n# and store it in another variable.\r\ngvnele = 'this'\r\n# Pass the given element to the index function for the given list\r\n# and store the result in resltind variable.\r\nresltind = gvnlst.index(gvnele)\r\n# Print the resltind value.\r\nprint('The given element {', gvnele, '} in the given list',\r\n gvnlst, 'is present at the index :')\r\nprint(resltind)\r\n<\/pre>\n
The given element { this } in the given list ['hello', 'this', 'is', 'python', 'programs'] is present at the index :\r\n1<\/pre>\n
Example-2: If the element is not present in the given list<\/h5>\n
# Give the list as the static input and store it in a variable.\r\ngvnlst = ['hello', 'this', 'is', 'python', 'programs']\r\n# Give the element whose index must be located in the given list\r\n# and store it in another variable.\r\ngvnele = 'btechgeeks'\r\n# Pass the given element to the index function for the given list\r\n# and store the result in resltind variable.\r\nresltind = gvnlst.index(gvnele)\r\n# Print the resltind value.\r\nprint('The given element {', gvnele, '} in the given list',\r\n gvnlst, 'is present at the index :')\r\nprint(resltind)\r\n<\/pre>\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
Example-3: Giving Start Index<\/h5>\n
# Give the list as the static input and store it in a variable.\r\ngvnlst = ['hello', 'this', 'is', 'python', 'programs']\r\n# Give the element whose index must be located in the given list\r\n# and store it in another variable.\r\ngvnele = 'python'\r\n# Pass the given element to the index function for the given list\r\n# and store the result in resltind variable.\r\n# searching the element after 1 st index by passing second argument as 1\r\nresltind = gvnlst.index(gvnele, 1)\r\n# Print the resltind value.\r\nprint('The given element {', gvnele, '} in the given list',\r\n gvnlst, 'is present at the index :')\r\nprint(resltind)\r\n<\/pre>\n
The given element { python } in the given list ['hello', 'this', 'is', 'python', 'programs'] is present at the index :\r\n3<\/pre>\n
Example-4: Giving Start and End Index<\/h5>\n
# Give the list as the static input and store it in a variable.\r\ngvnlst = ['hello', 'this', 'is', 'python', 'programs']\r\n# Give the element whose index must be located in the given list\r\n# and store it in another variable.\r\ngvnele = 'python'\r\n# Pass the given element to the index function for the given list\r\n# and store the result in resltind variable.\r\n# searching the element after 0 st index to 2 index by passing\r\n# second argument as 0 and third argument as 2\r\nresltind = gvnlst.index(gvnele, 0, 2)\r\n# Print the resltind value.\r\nprint('The given element {', gvnele, '} in the given list',\r\n gvnlst, 'is present at the index :')\r\nprint(resltind)\r\n<\/pre>\n
Traceback (most recent call last):\r\n File \"\/home\/e3f1e4ebf5b32326c6525257b0b59752.py\", line 10, in <module>\r\n resltind = gvnlst.index(gvnele, 0, 2)\r\nValueError: 'python' is not in list<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvnlst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the element whose index must be located in the given list\r\n# and store it in another variable.\r\ngvnele = int(input('Enter some random element = '))\r\n# Pass the given element to the index function for the given list\r\n# and store the result in resltind variable.\r\n\r\nresltind = gvnlst.index(gvnele)\r\n# Print the resltind value.\r\nprint('The given element {', gvnele, '} in the given list',\r\n gvnlst, 'is present at the index :')\r\nprint(resltind)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 6 2 8 9 1 8 4\r\nEnter some random element = 8\r\nThe given element { 8 } in the given list [6, 2, 8, 9, 1, 8, 4] is present at the index :\r\n2<\/pre>\n
\n