{"id":3125,"date":"2026-11-11T06:10:03","date_gmt":"2026-11-11T06:10:03","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3125"},"modified":"2026-11-11T06:10:03","modified_gmt":"2026-11-11T06:10:03","slug":"membership-operators-in-python","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/membership-operators-in-python\/","title":{"rendered":"Membership Operators in Python (in, not in) with Examples"},"content":{"rendered":"<p>Membership operators check whether a value is present inside a collection such as a list, string, tuple, set, dictionary or range. Python has two of them, <code>in<\/code> and <code>not in<\/code>, and both return <code>True<\/code> or <code>False<\/code>.<\/p>\n<p>They make code read almost like English: <code>if \"mango\" in fruits:<\/code> means exactly what it says.<\/p>\n<h2>Membership Operators in Python<\/h2>\n<table>\n<thead>\n<tr>\n<th>Operator<\/th>\n<th>Returns True when<\/th>\n<th>Example<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>in<\/code><\/td>\n<td>the value is found in the collection<\/td>\n<td><code>\"a\" in \"cat\"<\/code> gives <code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>not in<\/code><\/td>\n<td>the value is <strong>not<\/strong> found in the collection<\/td>\n<td><code>5 not in [1, 2, 3]<\/code> gives <code>True<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>With Lists<\/h2>\n<pre class=\"wp-block-code\"><code>fruits = [\"apple\", \"mango\", \"banana\"]\nprint(\"mango\" in fruits)\nprint(\"grapes\" in fruits)\nprint(\"grapes\" not in fruits)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nFalse\nTrue<\/code><\/pre>\n<h2>With Strings<\/h2>\n<p>For strings, <code>in<\/code> checks for a <strong>substring<\/strong>, so it can find whole words or parts of words. The check is case-sensitive.<\/p>\n<pre class=\"wp-block-code\"><code>sentence = \"Python is easy to learn\"\nprint(\"easy\" in sentence)\nprint(\"Easy\" in sentence)\nprint(\"\" in sentence)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nFalse\nTrue<\/code><\/pre>\n<p>An empty string is considered to be inside every string, which is why the last line is True. For a case-insensitive check, convert both sides: <code>\"Easy\".lower() in sentence.lower()<\/code>.<\/p>\n<h2>With Tuples and Sets<\/h2>\n<pre class=\"wp-block-code\"><code>vowels = (\"a\", \"e\", \"i\", \"o\", \"u\")\nprint(\"e\" in vowels)\n\nprimes = {2, 3, 5, 7, 11}\nprint(9 in primes)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nFalse<\/code><\/pre>\n<p>Sets are the fastest choice for membership checks. Python checks a set or a dictionary almost instantly, however large it is, while a list has to be scanned item by item. If you need to check membership many times in a big collection, convert the list to a set first.<\/p>\n<h2>With Dictionaries<\/h2>\n<p>With a dictionary, <code>in<\/code> checks the <strong>keys<\/strong>, not the values. Use <code>.values()<\/code> or <code>.items()<\/code> when you need those.<\/p>\n<pre class=\"wp-block-code\"><code>marks = {\"Riya\": 91, \"Aman\": 78}\nprint(\"Riya\" in marks)\nprint(91 in marks)\nprint(91 in marks.values())\nprint((\"Aman\", 78) in marks.items())<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nFalse\nTrue\nTrue<\/code><\/pre>\n<h2>With range()<\/h2>\n<pre class=\"wp-block-code\"><code>print(50 in range(1, 101))\nprint(100 in range(1, 100))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nFalse<\/code><\/pre>\n<p><code>range(1, 100)<\/code> stops before 100, so 100 is not in it.<\/p>\n<h2>Practical Examples<\/h2>\n<h3>1. Count the Vowels in a Word<\/h3>\n<pre class=\"wp-block-code\"><code>word = \"education\"\ncount = 0\nfor ch in word:\n    if ch in \"aeiou\":\n        count += 1\nprint(count)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>5<\/code><\/pre>\n<h3>2. Validate a Menu Choice<\/h3>\n<pre class=\"wp-block-code\"><code>choice = \"3\"\nif choice in (\"1\", \"2\", \"3\"):\n    print(\"Valid choice\")\nelse:\n    print(\"Invalid choice\")<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Valid choice<\/code><\/pre>\n<p>This is much cleaner than writing <code>choice == \"1\" or choice == \"2\" or choice == \"3\"<\/code>.<\/p>\n<h3>3. Remove Duplicates While Keeping the Order<\/h3>\n<pre class=\"wp-block-code\"><code>names = [\"Riya\", \"Aman\", \"Riya\", \"Kabir\", \"Aman\"]\nunique = []\nfor n in names:\n    if n not in unique:\n        unique.append(n)\nprint(unique)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>['Riya', 'Aman', 'Kabir']<\/code><\/pre>\n<h2>Watch Out: Nested Lists<\/h2>\n<p><code>in<\/code> only looks at the top level of a collection. It does not search inside inner lists.<\/p>\n<pre class=\"wp-block-code\"><code>matrix = [[1, 2], [3, 4]]\nprint(3 in matrix)\nprint([3, 4] in matrix)\nprint(any(3 in row for row in matrix))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>False\nTrue\nTrue<\/code><\/pre>\n<p>The items of <code>matrix<\/code> are the two inner lists, so <code>3 in matrix<\/code> is False. To search inside every row, use <code>any()<\/code> as in the last line.<\/p>\n<h2>Practice Questions<\/h2>\n<ol>\n<li><code>print(\"a\" in \"Banana\")<\/code><\/li>\n<li><code>print(\"ana\" in \"Banana\")<\/code><\/li>\n<li><code>print(2 in [[1, 2], [3]])<\/code><\/li>\n<\/ol>\n<p><strong>Answers:<\/strong> 1) <code>True<\/code>. &#8220;Banana&#8221; contains a small &#8220;a&#8221; at index 1. 2) <code>True<\/code>. &#8220;ana&#8221; appears as a substring. 3) <code>False<\/code>. The list holds two inner lists, not the number 2.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>What are membership operators in Python?<\/h3>\n<p><code>in<\/code> and <code>not in<\/code>. They test whether a value is present in a sequence or collection.<\/p>\n<h3>Does in check keys or values in a dictionary?<\/h3>\n<p>Keys. Use <code>value in my_dict.values()<\/code> to check values.<\/p>\n<h3>Is the in operator case-sensitive for strings?<\/h3>\n<p>Yes. <code>\"Easy\" in \"Python is easy\"<\/code> is False. Convert both strings to lower case for a case-insensitive check.<\/p>\n<h3>What is the difference between in and ==?<\/h3>\n<p><code>==<\/code> compares two whole values. <code>in<\/code> checks whether one value is contained inside another.<\/p>\n<p>Next: <a href=\"https:\/\/gyanipandit.com\/programming\/identity-operators-in-python\/\">Identity operators in Python<\/a> \u00b7 Previous: <a href=\"https:\/\/gyanipandit.com\/programming\/python-bitwise-operators\/\">Python bitwise operators<\/a> \u00b7 All operators: <a href=\"https:\/\/gyanipandit.com\/programming\/python-operators\/\">Python operators overview<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Membership operators check whether a value is present inside a collection such as a list, string, tuple, set, dictionary or range. Python has two of them, in and not in, and both return True or False. They make code read almost like English: if &#8220;mango&#8221; in fruits: means exactly what it says. Membership Operators in &#8230; <a title=\"Membership Operators in Python (in, not in) with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/membership-operators-in-python\/\" aria-label=\"Read more about Membership Operators in Python (in, not in) with Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":["post-3125","post","type-post","status-publish","format-standard","hentry","category-python-tutorial","tag-python","tag-python-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3125"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3125\/revisions"}],"predecessor-version":[{"id":6130,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3125\/revisions\/6130"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}