{"id":5976,"date":"2023-10-29T10:40:40","date_gmt":"2023-10-29T05:10:40","guid":{"rendered":"https:\/\/python-programs.com\/?p=5976"},"modified":"2023-11-10T12:05:37","modified_gmt":"2023-11-10T06:35:37","slug":"python-how-to-find-an-element-in-tuple-by-value","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-find-an-element-in-tuple-by-value\/","title":{"rendered":"Python : How to Find an Element in Tuple by Value"},"content":{"rendered":"
Tuples are variable types that allow you to store multiple items in a single variable. Tuple is one of Python’s four built-in data types for storing data collections. The other three are List, Set, and Dictionary, each with unique properties and applications. Tuples are collections that are both ordered and immutable.<\/p>\n
Given a tuple, the task is to determine whether or not a given element is present in the tuple.<\/p>\n
Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to search an element in tuple some of them are:<\/p>\n This is a brute force method for completing this task. We iterate through the tuple and check each element to see if it is given element. If the element matches then written true else return false.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Using the in<\/strong> operator is the most Pythonic way to accomplish this task. It is a one-liner that is recommended for this task.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Another method for searching for an element in a tuple is to use the not in operator.<\/p>\n If the element is not present in the tuple, it returns True otherwise, it returns False.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Tuple has another member function count(),<\/p>\n It returns the number of occurrences of element in the tuple.<\/p>\n If the count of an element is greater than zero, the element is present in the tuple .otherwise, the element is not present in the tuple.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Sometimes just checking if an element exists in a tuple isn’t enough ,we want to know where it appears for the first time in the tuple. A member function index is provided by Tuple () Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Tuples are variable types that allow you to store multiple items in a single variable. Tuple is one of Python’s four built-in data types for storing data collections. The other three are List, Set, and Dictionary, each with unique properties and applications. Tuples are collections that are both ordered and immutable. Given a tuple, the …<\/p>\ngiven_tuple =( 3,4,5,6,2) element =2<\/pre>\n
The item is found in the tuple<\/pre>\n
Search an element in Tuple by value<\/h2>\n
\n
Method #1:Using for loop<\/h3>\n
# function which returns true if given element is found in tuple\r\ndef searchElement(given_tuple, element):\r\n # using for loop to traverse the tuple\r\n for value in given_tuple:\r\n # if the given element is equal to the value then return true\r\n if(value == element):\r\n return True\r\n # if the element is not found in tuple then return False\r\n return False\r\n\r\n\r\n# given tuple\r\ngiven_tuple = (3, 4, 5, 6, 2)\r\n# given element\r\nelement = 2\r\n# passing tuple and element to searchElement function\r\nif(searchElement(given_tuple, element)):\r\n print(\"The item is found in the tuple\")\r\nelse:\r\n print(\"The\u00a0specified\u00a0element\u00a0does\u00a0not\u00a0exist\u00a0in\u00a0the\u00a0tuple\")\r\n<\/pre>\n
The item is found in the tuple<\/pre>\n
Method #2:Using in operator<\/h3>\n
# function which returns true if given element is found in tuple\r\ndef searchElement(given_tuple, element):\r\n #using in operator\r\n # if the given eleement is present then return true\r\n if element in given_tuple:\r\n return True\r\n else:\r\n return False\r\n\r\n\r\n# given tuple\r\ngiven_tuple = (3, 4, 5, 6, 2)\r\n# given element\r\nelement = 2\r\n# passing tuple and element to searchElement function\r\nif(searchElement(given_tuple, element)):\r\n print(\"The item is found in the tuple\")\r\nelse:\r\n print(\"The\u00a0specified\u00a0element\u00a0does\u00a0not\u00a0exist\u00a0in\u00a0the\u00a0tuple\")\r\n<\/pre>\n
The item is found in the tuple<\/pre>\n
Method #3:Using not in operator<\/h3>\n
# function which returns true if given element is found in tuple\r\ndef searchElement(given_tuple, element):\r\n #using in operator\r\n # if the given eleement is not present then return false\r\n if element not in given_tuple:\r\n return False\r\n else:\r\n return True\r\n\r\n\r\n# given tuple\r\ngiven_tuple = (3, 4, 5, 6, 2)\r\n# given element\r\nelement = 2\r\n# passing tuple and element to searchElement function\r\nif(searchElement(given_tuple, element)):\r\n print(\"The item is found in the tuple\")\r\nelse:\r\n print(\"The\u00a0specified\u00a0element\u00a0does\u00a0not\u00a0exist\u00a0in\u00a0the\u00a0tuple\")\r\n<\/pre>\n
The item is found in the tuple<\/pre>\n
Method #4:Using count() function<\/h3>\n
given_tuple.count(element)<\/code><\/p>\n
# function which returns true if given element is found in tuple\r\ndef searchElement(given_tuple, element):\r\n # using count() function to count the element in tuple\r\n element_count = given_tuple.count(element)\r\n # if the count is greater than 0 then return True\r\n if element_count > 0:\r\n return True\r\n else:\r\n return False\r\n\r\n\r\n# given tuple\r\ngiven_tuple = (3, 4, 5, 6, 2)\r\n# given element\r\nelement = 2\r\n# passing tuple and element to searchElement function\r\nif(searchElement(given_tuple, element)):\r\n print(\"The item is found in the tuple\")\r\nelse:\r\n print(\"The\u00a0specified\u00a0element\u00a0does\u00a0not\u00a0exist\u00a0in\u00a0the\u00a0tuple\")\r\n<\/pre>\n
The item is found in the tuple<\/pre>\n
Method #5:Using index function<\/h3>\n
\nIt returns the index of the first occurrence of x in the tuple. In addition, if an element is not found in a tuple, an exception ValueError is thrown.<\/p>\n# given tuple\r\ngiven_tuple = (3, 4, 5, 6, 2)\r\n# given element\r\nelement = 2\r\ntry:\r\n # calculating the element's index\r\n elem_index = given_tuple.index(element)\r\n # printing the position\r\n print(\"The given element is found at the index\", elem_index)\r\nexcept:\r\n print(\"The specified element does not exist in the tuple\")\r\n<\/pre>\n
The item is found in the tuple<\/pre>\n
\n