# Check if element exists in list in Python

## Check if element exists in list in Python

List is an important container in python as if stores elements of all the datatypes as a collection. Knowledge of certain list operations is necessary for day-day programming. This article discusses one of the basic list operations of ways to check the existence of elements in the list.

## Method 1: Naive Method

In Naive method, one easily uses a loop that iterates through all the elements to check the existence of the target element. This is the simplest way to check the existence of the element in the list.

## Method 2: Using in

Python is the most conventional way to check if an element exists in a list or not. This particular way returns True if an element exists in the list and False if the element does not exist in the list. List need not be sorted to practice this approach of checking.
Code #1: Demonstrating to check the existence of an element in the list using the Naive method and in .

 `# Python code to demonstrate` `# checking of element existence` `# using loops and in` `# Initializing list` `test_list ``=` `[ ``1``, ``6``, ``3``, ``5``, ``3``, ``4` `]` `print``(``"Checking if 4 exists in list ( using loop ) : "``)` `# Checking if 4 exists in list` `# using loop` `for` `i ``in` `test_list:` `    ``if``(i ``=``=` `4``) :` `        ``print` `(``"Element Exists"``)` `print``(``"Checking if 4 exists in list ( using in ) : "``)` `# Checking if 4 exists in list` `# using in` `if` `(``4` `in` `test_list):` `    ``print` `(``"Element Exists"``)`

Output :

```Checking if 4 exists in list ( using loop ) :
Element Exists
Checking if 4 exists in list ( using in ) :
Element Exists```

## Method 3 : Using set() + in

Converting the list into the set and then using in can possibly be more efficient than only using in. But having efficiency for a plus also has certain negatives. One among them is that the order of list is not preserved, and if you opt to take a new list for it, you would require to use extra space. Another drawback is that set disallows duplicity and hence duplicate elements would be removed from the original list.

## Method 4 : Using sort() + bisect_left()

The conventional binary search way of testing element existence, hence list has to be sorted first and hence not preserving the element ordering. bisect_left() returns the first occurrence of the element to be found and has worked similarly to lower_bound() in C++ STL.

Note: The bisect function will only state the position of where to insert the element but not the details about if the element is present or not.

Code #2 : Demonstrating to check existence of element in list using set() + in and sort() + bisect_left().

 `# Python code to demonstrate` `# checking of element existence` `# using set() + in` `# using sort() + bisect_left()` `from` `bisect ``import` `bisect_left ,bisect` `# Initializing list` `test_list_set ``=` `[ ``1``, ``6``, ``3``, ``5``, ``3``, ``4` `]` `test_list_bisect ``=` `[ ``1``, ``6``, ``3``, ``5``, ``3``, ``4` `]` `print``(``"Checking if 4 exists in list ( using set() + in) : "``)` `# Checking if 4 exists in list` `# using set() + in` `test_list_set ``=` `set``(test_list_set)` `if` `4` `in` `test_list_set :` `    ``print` `(``"Element Exists"``)` `print``(``"Checking if 4 exists in list ( using sort() + bisect_left() ) : "``)` `# Checking if 4 exists in list` `# using sort() + bisect_left()` `test_list_bisect.sort()` `if` `bisect_left(test_list_bisect, ``4``)!``=``bisect(test_list_bisect, ``4``):` `    ``print` `(``"Element Exists"``)` `else``:` `    ``print``(``"Element doesnt exist"``)`

## Method 5 : Using count()

We can use the in-built python List method, count(), to check if the passed element exists in List. If the passed element exists in the List, count() method will show the number of times it occurs in the entire list. If it is a non-zero positive number, it means an element exists in the List.

Code #3 : Demonstrating to check the existence of elements in the list using count().

 `"""` `Python code to demonstrate` `checking of element existence` `using List count() method` `"""` `# Initializing list` `test_list ``=` `[``10``, ``15``, ``20``, ``7``, ``46``, ``2808``]` `print``(``"Checking if 15 exists in list"``)` `# number of times element exists in list` `exist_count ``=` `test_list.count(``15``)` `# checking if it is more then 0` `if` `exist_count > ``0``:` `    ``print``(``"Yes, 15 exists in list"``)` `else``:` `    ``print``(``"No, 15 does not exists in list"``)`

Last Updated on August 30, 2021 by admin

## Python program to read file word by wordPython program to read file word by word

Python program to read file word by word Given a text file and the task

## Python – os.path.isfile() methodPython – os.path.isfile() method

Python | os.path.isfile() method OS module in Python provides functions for interacting with the operating system.

## Python – Output FormattingPython – Output Formatting

Python – Output Formatting There are several ways to present the output of a program,

## Python list pop() MethodPython list pop() Method

Python list | pop() Python list pop() is an inbuilt function in Python that removes and returns

## Python – Convert JSON to stringPython – Convert JSON to string

Python – Convert JSON to string Data in transmitted across platforms using API calls. Data

## Python – How to sort a list of stringsPython – How to sort a list of strings

Python | How to sort a list of strings Given a list of strings, the

## What is the maximum possible value of an integer in Python ?What is the maximum possible value of an integer in Python ?

What is the maximum possible value of an integer in Python ? Consider below Python

## 3D Scatter Plotting in Python using Matplotlib3D Scatter Plotting in Python using Matplotlib

3D Scatter Plotting in Python using Matplotlib A 3D Scatter Plot is a mathematical diagram, the most