{"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>
\nTuples in Python:<\/strong><\/p>\n

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

gvntuple .index(element, start, end)<\/pre>\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.
\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 Tuple= ('hello', 'this', 'is', 'this', 'python', 'programs')\r\nGiven Element = 'this'<\/pre>\n

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

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

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

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

Given Tuple= ('hello', 'this', 'is', 'this', 'python', 'programs')\r\nGiven Element = 'morning'<\/pre>\n

Output:<\/strong><\/p>\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