{"id":24580,"date":"2021-10-22T09:02:48","date_gmt":"2021-10-22T03:32:48","guid":{"rendered":"https:\/\/python-programs.com\/?p=24580"},"modified":"2021-11-05T19:43:06","modified_gmt":"2021-11-05T14:13:06","slug":"python-program-to-find-index-of-a-tuple-item","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-index-of-a-tuple-item\/","title":{"rendered":"Python Program to Find Index of a Tuple Item"},"content":{"rendered":"
In the previous article, we have discussed Python Program for memoryview() Function<\/a> Syntax:<\/strong><\/p>\n If you specify a start value, the index function will begin searching from that point. Similarly, setting the end position causes the tuple index function to stop looking at that number.<\/p>\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 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 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 Program for memoryview() Function The tuple index function in Python returns the index of a given tuple item. The index position of the first found value is returned by the tuple index function. Syntax: TupleName.index(tupleValue, start, end) If you specify a start value, the index function will …<\/p>\n
\nThe tuple index function in Python returns the index of a given tuple item. The index position of the first found value is returned by the tuple index function.<\/p>\nTupleName.index(tupleValue, start, end)<\/pre>\n
Given tuple = (10, 20, 30, 50, 70, 20)\r\nGiven first number = 50\r\nGiven second number = 20\r\nGiven start position = 2<\/pre>\n
The given number's 50 index position = 3\r\nThe given number's 20 index position after the given start position 2 = 5<\/pre>\n
Given tuple = (1, 3, 5, 7, 8, 2, 5)\r\nGiven first number = 3\r\nGiven second number = 5\r\nGiven start position = 3<\/pre>\n
The given number's 3 index position = 1\r\nThe given number's 5 index position after the given start position 3 = 6<\/pre>\n
Program to Find Index of a Tuple Item in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the tuple as static input and store it in a variable.\r\ngvn_tupl = (10, 20, 30, 50, 70, 20)\r\n# Give the number as static input and store it in another variable.\r\ngvn_numb1 = 50\r\n# Get the index position of the given number using the index() method and\r\n# store it in another variable.\r\nindx_1 = gvn_tupl.index(gvn_numb1)\r\n# Print the index position of the given number.\r\nprint(\"The given number's\", gvn_numb1, \"index position = \", indx_1)\r\n# Give the second number as static input and store it in another variable.\r\ngvn_numb2 = 20\r\n# Give the start position as static input and store it in another variable.\r\ngvn_strtpositn = 2\r\n# Pass the given second number, start position as arguments to the index()\r\n# method to get the index value of the given second number after the given\r\n# start position.\r\n# Store it in another variable.\r\nindx_2 = gvn_tupl.index(gvn_numb2, gvn_strtpositn)\r\n# Print the index position of the given second number after the given\r\n# start position.\r\nprint(\"The given number's\", gvn_numb2,\r\n \"index position after the given start position\", gvn_strtpositn, \"= \", indx_2)\r\n<\/pre>\n
The given number's 50 index position = 3\r\nThe given number's 20 index position after the given start position 2 = 5<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the tuple user input using list(),map(),input(),and split() functions and store it in a variable.\r\ngvn_tupl = tuple(map(int, input(\r\n 'Enter some random Tuple Elements separated by spaces = ').split()))\r\n# Give the number as user input using the int(input()) function and store it in another variable.\r\ngvn_numb1 = int(input(\"Enter some random number = \"))\r\n# Get the index position of the given number using the index() method and\r\n# store it in another variable.\r\nindx_1 = gvn_tupl.index(gvn_numb1)\r\n# Print the index position of the given number.\r\nprint(\"The given number's\", gvn_numb1, \"index position = \", indx_1)\r\n# Give the second number as user input using the int(input()) function and\r\n# store it in another variable.\r\ngvn_numb2 = int(input(\"Enter some random number = \"))\r\n# Give the start position as user input using the int(input()) function and\r\n# store it in another variable.\r\ngvn_strtpositn = int(input(\"Enter some random number = \"))\r\n# Pass the given second number, start position as arguments to the index()\r\n# method to get the index value of the given second number after the given\r\n# start position.\r\n# Store it in another variable.\r\nindx_2 = gvn_tupl.index(gvn_numb2, gvn_strtpositn)\r\n# Print the index position of the given second number after the given\r\n# start position.\r\nprint(\"The given number's\", gvn_numb2,\r\n \"index position after the given start position\", gvn_strtpositn, \"= \", indx_2)\r\n<\/pre>\n
Enter some random Tuple Elements separated by spaces = 1 3 5 7 8 2 5\r\nEnter some random number = 3\r\nThe given number's 3 index position = 1\r\nEnter some random number = 5\r\nEnter some random number = 3\r\nThe given number's 5 index position after the given start position 3 = 6\r\n<\/pre>\n
Program to Find Index of a Tuple Item in Python without using index() Function<\/h4>\n
gvn_tupl = (10, 40, 50, 30, 60, 40, 20, 40, 30)\r\nprint(gvn_tupl)\r\n\r\ngvn_valu = 40\r\nfor itr in range(len(gvn_tupl)):\r\n if gvn_tupl[itr] == gvn_valu:\r\n print(\"The given number\", gvn_valu, \"index position = \", itr)\r\n break\r\nprint(\"The all Index Positions of given number:\")\r\nfor k in range(len(gvn_tupl)):\r\n if gvn_tupl[k] == gvn_valu:\r\n print(\"Index Position of 20 = \", k)\r\n\r\nprint(\"The all the Index Positions of given number using enumerate function:\")\r\nfor itor, p in enumerate(gvn_tupl):\r\n if p == gvn_valu:\r\n print(\"Index Position of 20 = \", itor)\r\n<\/pre>\n
(10, 40, 50, 30, 60, 40, 20, 40, 30)\r\nThe given number 40 index position = 1\r\nThe all Index Positions of given number:\r\nIndex Position of 20 = 1\r\nIndex Position of 20 = 5\r\nIndex Position of 20 = 7\r\nThe all the Index Positions of given number using enumerate function:\r\nIndex Position of 20 = 1\r\nIndex Position of 20 = 5\r\nIndex Position of 20 = 7<\/pre>\n
\n