How to Check if an Item Exists in List Using Python?

Share This Post

Is there a way which used to check if an item exists in the list in Python? Of course, what would be that if something already available in the list? In this post, I am going to describe that how to exactly do it?

There are about two to three ways that may help. In the first step, you will know by using operator, the second way need to use count() function, and in the third, we will use custom logic how to actually do it?

How to Check if an Item Exists in List Using Python? 3 Ways

First of all, did you already make any list yourself in Python? See out the sample, for example how to make a list in Python?


wholeblog = ['Hello' , 'What's Up', 'WholeBlog', 'TheWholeBlog', 'What', 'Done']

You can make list like the above example and also use in replace of ‘ ‘ = “, like


wholeblog = ["Hello", "What's Up", "WholeBlogs", "TheWholeBlog"]

Check if a value exists in the list using the operator

if 'hello' in wholeblog:
     print("Yes, 'hello' found in List : " , wholeblog)
Here it will show you out that Yes, the Hello is available in the list.

Check if a value exists in the list using count()

As described already that you will come to know also by using the count() build-in function in Python.


if wholeblog.count('hello') > 0 :

     print("Yes, 'hello' found in List : " , wholeblog)

That’s it.

Check if a value exists in the list using logic


result = any(len(elem) == 5 for elem in wholeblog)

if result:

    print("Yes, string element with size 5 found")

In the above example, you will come to know how to find out length also using custom logic.

Are you getting errors while installing difflib? follow up: How To Install difflib in Python Using CMD? this guide will help and resolve your errors whatever you are facing.

spot_img

Related Posts

Python for Loop Iteration: How to Easily Manipulate a List

One of the most powerful features of the Python...

How To Get First Characters of a String in Python

Way to find the first characters of a string...

How To Convert Tuple To String In Python

In this post, you will learn how to convert...

How To Convert String to Double in Python

Good to hear, that you want to convert a...

Python TypeError: String Index Out Of Range Solution

A string is an array of characters and part...

How To Remove Empty Strings From a List Of Strings

On this page, we will discuss how to remove...
- Advertisement -spot_img