{"id":24744,"date":"2021-11-02T09:45:52","date_gmt":"2021-11-02T04:15:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=24744"},"modified":"2021-11-05T19:46:15","modified_gmt":"2021-11-05T14:16:15","slug":"python-tuple-index-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-tuple-index-method-with-examples\/","title":{"rendered":"Python Tuple index() method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Tuple count() method with Examples<\/a> Tuples are a data structure in Python that stores an ordered succession of values. They are unchangeable. This signifies that the values of a tuple cannot be changed. They allow you to save an organized list of items. A tuple, for example, can be used to hold a list of employee names.<\/p>\n Tuple index() Method in Python:<\/strong><\/p>\n The index() method returns the position in the given tuple of the given element.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values:\u00a0<\/strong><\/p>\n The tuple 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 tuple 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:<\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Find a Comprehensive Collection of Python Built in Functions<\/a> that you need to be aware of and use them as a part of your program.<\/p>\n In the previous article, we have discussed Python Tuple count() method with Examples Tuples in Python: Tuples are a data structure in Python that stores an ordered succession of values. They are unchangeable. This signifies that the values of a tuple cannot be changed. They allow you to save an organized list of items. A …<\/p>\n
\nTuples in Python:<\/strong><\/p>\ngvntuple .index(element, start, end)<\/pre>\n
\nA ValueError exception is thrown if the element is not found.<\/p>\nGiven Tuple= ('hello', 'this', 'is', 'this', 'python', 'programs')\r\nGiven Element = 'this'<\/pre>\n
The given element { this } in the given tuple ('hello', 'this', 'is', 'this', 'python', 'programs') is present at the index :\r\n1<\/pre>\n
Given Tuple= ('hello', 'this', 'is', 'this', 'python', 'programs')\r\nGiven Element = 'morning'<\/pre>\n
Traceback (most recent call last):\r\n File \"\/home\/a3ee758dfee37d1702ad5c70e38293aa.py\", line 8, in <module>\r\n resltind = gvntupl.index(gvnele)\r\nValueError: tuple.index(x): x not in tuple<\/pre>\n
Tuple 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 Tuple<\/h5>\n
\n
# Give the tuple as the static input and store it in a variable.\r\ngvntupl = ('hello', 'this', 'is', 'this', 'python', 'programs')\r\n# Give the element whose index must be located in the given tuple\r\n# and store it in another variable.\r\ngvnele = 'this'\r\n# Pass the given element to the index function for the given tuple\r\n# and store the result in resltind variable.\r\nresltind = gvntupl.index(gvnele)\r\n# Print the resltind value.\r\nprint('The given element {', gvnele, '} in the given tuple',\r\n gvntupl, 'is present at the index :')\r\nprint(resltind)\r\n<\/pre>\n
The given element { this } in the given tuple ('hello', 'this', 'is', 'this', 'python', 'programs') is present at the index :\r\n1<\/pre>\n
Example-2: If the element is not present in the given Tuple<\/h5>\n
# Give the tuple as the static input and store it in a variable.\r\ngvntupl = ('hello', 'this', 'is', 'this', 'python', 'programs')\r\n# Give the element whose index must be located in the given tuple\r\n# and store it in another variable.\r\ngvnele = 'now'\r\n# Pass the given element to the index function for the given tuple\r\n# and store the result in resltind variable.\r\nresltind = gvntupl.index(gvnele)\r\n# Print the resltind value.\r\nprint('The given element {', gvnele, '} in the given tuple',\r\n gvntupl, 'is present at the index :')\r\nprint(resltind)\r\n<\/pre>\n
Traceback (most recent call last):\r\n File \"\/home\/a3ee758dfee37d1702ad5c70e38293aa.py\", line 8, in <module>\r\n resltind = gvntupl.index(gvnele)\r\nValueError: tuple.index(x): x not in tuple<\/pre>\n
Example-3: Giving Start Index<\/h5>\n
# Give the tuple as the static input and store it in a variable.\r\ngvntupl = ('hello', 'this', 'is', 'this', 'python', 'programs')\r\n# Give the element whose index must be located in the given tuple\r\n# and store it in another variable.\r\ngvnele = 'this'\r\n# Pass the given element to the index function for the given tuple\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 = gvntupl.index(gvnele, 1)\r\n# Print the resltind value.\r\nprint('The given element {', gvnele, '} in the given tuple',\r\n gvntupl, 'is present at the index :')\r\nprint(resltind)\r\n<\/pre>\n
The given element { this } in the given tuple ('hello', 'this', 'is', 'this', 'python', 'programs') is present at the index :\r\n1<\/pre>\n
Example-4: Giving Start and End Index<\/h5>\n
# Give the tuple as the static input and store it in a variable.\r\ngvntupl = ('hello', 'this', 'is', 'this', 'python', 'programs')\r\n# Give the element whose index must be located in the given tuple\r\n# and store it in another variable.\r\ngvnele = 'hello'\r\n# Pass the given element to the index function for the given tuple\r\n# and store the result in resltind variable.\r\n# searching the element after 1 st index to 3 index by passing\r\n# second argument as 1 and third argument as 3\r\nresltind = gvntupl.index(gvnele, 1, 3)\r\n# Print the resltind value.\r\nprint('The given element {', gvnele, '} in the given tuple',\r\n gvntupl, 'is present at the index :')\r\nprint(resltind)\r\n<\/pre>\n
Traceback (most recent call last):\r\n File \"\/home\/0eabc1fa82ac939d529fa63ad0fd9be6.py\", line 10, in <module>\r\n resltind = gvntupl.index(gvnele, 1, 3)\r\nValueError: tuple.index(x): x not in tuple<\/pre>\n
The element hello is present at the 0th index but not between the indices, resulting in an error.<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the Integer tuple as user input using tuple (),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvntupl = tuple(map(int, input('Enter some random tuple elements = ').split()))\r\n# Give the element as user input using the int(), input() functions,\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 tuple\r\n# and store the result in resltind variable.\r\nresltind = gvntupl.index(gvnele)\r\n# Print the resltind value.\r\nprint('The given element {', gvnele, '} in the given tuple',\r\n gvntupl, 'is present at the index :')\r\nprint(resltind)\r\n<\/pre>\n
Enter some random tuple elements = 4 1 7 8 3 11 7 3\r\nEnter some random element = 3\r\nThe given element { 3 } in the given tuple (4, 1, 7, 8, 3, 11, 7, 3) is present at the index :\r\n4<\/pre>\n
\n