{"id":2778,"date":"2023-10-17T09:39:00","date_gmt":"2023-10-17T04:09:00","guid":{"rendered":"https:\/\/python-programs.com\/?p=2778"},"modified":"2023-11-10T11:44:54","modified_gmt":"2023-11-10T06:14:54","slug":"python-how-to-check-if-an-item-exists-in-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/","title":{"rendered":"Python : How to Check if an item exists in list ?"},"content":{"rendered":"

How to Check if an item exists in list using Python?<\/h2>\n

In Python List<\/strong> is a most versatile datatype which stores multiple items in a single variable. All the items present inside a list are indexed where the first item is associated with the index position 0 (Zero). In this article we will discuss how to check whether the list contains a specific item or not. Let’s explore the concept in more detail.<\/p>\n

Example of a List :<\/p>\n

List_item = [ 'Bhubaneswar' , 'Mumbai' , 'Kolkatta' , 'Chennai' , 'Delhi' ]<\/pre>\n

So, our task is to check an item that is present in the list or not. Suppose we are looking for the item ‘Mumbai’<\/em> in List_item<\/code> and it is present in the list but if we will look for the item ‘Goa’<\/em> that does not exist in the list named List_item.<\/code><\/p>\n

There are multiple approaches available which can be used to achieve this. So, let’s discuss each approach one by one.<\/p>\n

Method-1 : Checking if the item exists in the list using “in” operator<\/h3>\n

\"in\" operator<\/code> in python can be used to check the item exists in the list or not. With the help of if condition<\/code> it can check whether the item is present or not. If it is present in the list then it returns True<\/code> and if it is not present in the list it returns False<\/code>.<\/p>\n

Syntax : item_name in list_name<\/pre>\n
#Program :\r\n\r\nList_item = [ 'Bhubaneswar' , 'Mumbai' , 'Kolkatta' , 'Chennai' , 'Delhi' ]\r\n#Checking an item which is present in the list\r\nif 'Mumbai' in List_item:\r\n    print('Item Mumbai is present')\r\nelse:\r\n    print('Item Mumbai is not present')\r\n#Checking an item which is not present in the list\r\nif 'Goa' in List_item:\r\n    print('Item Goa is present')\r\nelse:\r\n    print('Item Goa is not present')\r\n<\/pre>\n
Output :\r\nItem Mumbai is present\r\nItem Goa is not present<\/pre>\n

Method-2 : Checking if the item exists in list using list.count() function<\/h3>\n

list.count()<\/code> method gives the occurrence of an item in the list. Means if the item found at least once or more than that then the item is present if the item occurrence is zero then that item is not present in the list.<\/p>\n

Synatx :  item_name.count<\/span>(item)<\/pre>\n
#Program :\r\n\r\nList_item = [ 'Bhubaneswar' , 'Mumbai' , 'Kolkatta' , 'Chennai' , 'Delhi' ]\r\n#Checking an item which is present in the list\r\n#Occurrence of Chennai is 1\r\nif List_item.count('Chennai'):\r\n    print('Item Chennai is present')\r\nelse:\r\n    print('Item Chennai is not present')\r\n#Checking an item which is not present in the list\r\n#Occurrence of Pune is 1\r\nif List_item.count('Pune'):\r\n    print('Item Pune is present')\r\nelse:\r\n    print('Item Pune is not present')<\/pre>\n
Output :\r\nItem Chennai is present\r\nItem Pune is not present<\/pre>\n

Method-3 : Checking if the item exists in list using any() function<\/h3>\n

Using any( ) function<\/code> for checking a string is a most classical way of performing this task. With the help of any( ) function<\/code> we can check if any item of given iterable is True or we can check for a match in a string with a match of each list item\/element.<\/p>\n

Syntax : any(condition)<\/pre>\n
Python :\r\n\r\nList_item = [ 'Bhubaneswar' , 'Mumbai' , 'Kolkatta' , 'Chennai' , 'Delhi' ]\r\n\r\n#Checking an item which is present in the list\r\n#Checking any item whose length is 11 exist in the list or not\r\nresult = any(len(item) == 11 for item in List_item)\r\nif result:\r\n    print('Item Bhubaneswar is present')\r\nelse:\r\n    print('Item bhubaneswar is not present')\r\n\r\n#Checking an item which is not present in the list\r\n#Checking any item whose length is 1 exist in the list or not\r\nresult = any(len(item) == 1 for item in List_item)\r\nif result:\r\n    print('Item found with length 1')\r\nelse:\r\n    print('No Item found with length 1')\r\n<\/pre>\n
Output :\r\nItem Bhubaneswar is present\r\nNo Item found with length 1<\/pre>\n

Method-4 : Checking if the item exists in list using ‘not in’ inverse operator<\/h3>\n

'not in'<\/code> inverse operator in python is an inbuilt operator which returns True if the item is not present in the list and it returns False if the item is present in the list.<\/p>\n

Syntax : item_name not in list_name<\/pre>\n
Program :\r\n\r\nList_item = [ 'Bhubaneswar' , 'Mumbai' , 'Kolkatta' , 'Chennai' , 'Delhi' ] \r\n#Checking an item which is not present in the list\r\n#It will result true because Goa is not present in list\r\nif 'Goa' not in List_item: \r\n    print('True, Goa not in list') \r\nelse :\r\n    print('False, Goa is in list') \r\n\r\n#Checking an item which is present in the list \r\n#It will result false because Delhi is present in list\r\nif 'Delhi' not in List_item: \r\n    print('True, Delhi not in list') \r\nelse :\r\n    print('False, Delhi is in list')<\/pre>\n
Output :\r\nTrue, Goa not in list\r\nFalse, Delhi is in list<\/pre>\n

Method-5 : Checking if the item exists in list using Naive method<\/h3>\n

The naive method is the most simplest way of checking the existance of an element as it iterates the through all the elements of the list. If the item found in the list then it return true.<\/p>\n

#Program :\r\n\r\nList_item = [ 'Bhubaneswar' , 'Mumbai' , 'Kolkatta' , 'Chennai' , 'Delhi' ] \r\n#Checking an item which is not present in the list\r\n#It will result true because Kolkatta present in list\r\n\r\nfor i in List_item: \r\n    if i == 'Kolkatta':\r\n        print(\"True\")<\/pre>\n
Output :\r\nTrue<\/pre>\n","protected":false},"excerpt":{"rendered":"

How to Check if an item exists in list using Python? In Python List is a most versatile datatype which stores multiple items in a single variable. All the items present inside a list are indexed where the first item is associated with the index position 0 (Zero). In this article we will discuss how …<\/p>\n

Python : How to Check if an item exists in list ?<\/span> Read More »<\/a><\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython : How to Check if an item exists in list ? - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python : How to Check if an item exists in list ? - Python Programs\" \/>\n<meta property=\"og:description\" content=\"How to Check if an item exists in list using Python? In Python List is a most versatile datatype which stores multiple items in a single variable. All the items present inside a list are indexed where the first item is associated with the index position 0 (Zero). In this article we will discuss how … Python : How to Check if an item exists in list ? Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-17T04:09:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:14:54+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Satyabrata Jena\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/\",\"name\":\"Python : How to Check if an item exists in list ? - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-17T04:09:00+00:00\",\"dateModified\":\"2023-11-10T06:14:54+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python : How to Check if an item exists in list ?\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\"},\"headline\":\"Python : How to Check if an item exists in list ?\",\"datePublished\":\"2023-10-17T04:09:00+00:00\",\"dateModified\":\"2023-11-10T06:14:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#webpage\"},\"wordCount\":422,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\",\"name\":\"Satyabrata Jena\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"caption\":\"Satyabrata Jena\"},\"url\":\"https:\/\/python-programs.com\/author\/satyabrata\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python : How to Check if an item exists in list ? - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/","og_locale":"en_US","og_type":"article","og_title":"Python : How to Check if an item exists in list ? - Python Programs","og_description":"How to Check if an item exists in list using Python? In Python List is a most versatile datatype which stores multiple items in a single variable. All the items present inside a list are indexed where the first item is associated with the index position 0 (Zero). In this article we will discuss how … Python : How to Check if an item exists in list ? Read More »","og_url":"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-17T04:09:00+00:00","article_modified_time":"2023-11-10T06:14:54+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Satyabrata Jena","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#webpage","url":"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/","name":"Python : How to Check if an item exists in list ? - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-17T04:09:00+00:00","dateModified":"2023-11-10T06:14:54+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python : How to Check if an item exists in list ?"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd"},"headline":"Python : How to Check if an item exists in list ?","datePublished":"2023-10-17T04:09:00+00:00","dateModified":"2023-11-10T06:14:54+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#webpage"},"wordCount":422,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-how-to-check-if-an-item-exists-in-list\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd","name":"Satyabrata Jena","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","caption":"Satyabrata Jena"},"url":"https:\/\/python-programs.com\/author\/satyabrata\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/2778"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=2778"}],"version-history":[{"count":3,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/2778\/revisions"}],"predecessor-version":[{"id":2831,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/2778\/revisions\/2831"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=2778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=2778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=2778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}