Python Program To Search An Element In A List


Python is an interpreted, object–oriented, high level, programming language with dynamic semantics. Developed by Gudio Van Rossum in 1991. It supports multiple programming paradigms, including structured, object-oriented and functional programming.

List are mutable means that you can change the values of list, like we can add an element, remove, repeat and allows indexing and slicing like strings an element from a list by using different operators.

List can be created by using square brackets. By placing elements inside square bracket[ ],separate by commas. For example: list1= [“a”,”b”]. There is a one more type of list known as “Nested list”. It is list within list.

For example: list1=[“a”,”b”[1,2]”c”]

Searching an Element in a List

To locate the index or position of the element in a list is important when trying to access, manipulate or delete specific items from the list. Read further to learn how to search an element in a list using python.

The examples mentioned below help you to understand how to enter a list of elements and then search for a particular item within that list. This helps in finding items rapidly, as you no longer have to go through them manually one by one.

Here, we have given a list of numbers and we have to search an element from a list by using different methods. There eight methods to remove a subset from a list.

  • Using in operator

  • Using if else statement

  • Using loop

  • Using any()

  • Using find()

  • Using counter()

  • Using count()

Using“ if else”Statement

To find an element in a list using an if-else statement, simply loop through each element in the list and check if it is equal to the element you are looking for. It returns True if the condition is satisfied or, false otherwise.

Example

In the following example, we are using “if else” statement to search for the element in the list given below. The list is created with six elements, the variable ‘i’ is set to equal “arun”. Now, the if-else statement comes into scenario and checks whether or not the value of ‘I’ exists in the list.

lst=["kiran","arun","varun","kunnal","tiya","rhea" ]
i="arun"
if i in lst:
   print("exist")
else:
   print("not exist")

Output

Following is the output for the above program, when executed -

exist

Example

In the following example, we are using the if-else statement to search for the element “arjun” which is set by variable i. Here, the list 'lst' contains 6 strings.

lst=["kiran","arun","varun","kunnal","tiya","rhea" ]
i="arjun"
if i in lst:
   print("exist")
else:
   print("not exist")

Output

Following is the output, when we execute the above code.

not exist

Here, we have two programs in both we have used “ if else” statement to finding an element .In first program the we have search “arun” element that exists in a list .so, outcome comes as “exist”. And in second we have to search an item “arjun’ which does not exist in a list. So,output comes as “not exist’

Using in Operator

The in operator is useful for finding an element in a list. It checks whether a given element is in the list or not. This method returns True if the element is found or False otherwise. This makes searching for items quicker and easier than manually looking through each item in the list.

The “in” operator determines whether a given value is constituent element of a sequence such as a string, array, list, or tuple. It is used to search an element in a list.

Example

Here, we have a program in which we have used “in” operator to finding an element .In program, we have to search “kunnal” element that exists in a list. So, outcome comes as “element exist”

lst=["kiran","arun","varun","kunnal","tiya","rhea" ]
if ("kunnal"in lst):
   print("Element Exists")

Output

Following output is generated upon executing the above program.

Element Exists

Here, we have a program in which we have used “in” operator to finding an element .In program, we have to search “kunnal” element that exists in a list. So, outcome comes as “element exist”

Using for Loop

A for loop is used to execute a statement repeatedly until given condition is satisfied. And when condition becomes false, the line immediately after the loop in the program is executed.

Example

In the following program, we are using “for loop” method to find an element we are looking for. In the program, variable 'lst' is assigned a list of names. The for loop iterates through each element in the list and checks if it equals "varun".

lst=["kiran","arun","varun","kunnal","tiya","rhea" ]
for i in lst:
   if(i == "varun"):
      print("Element Exists")

Output

We have a program in which we have used “for loop” for finding an element .In this program, we have to search “varun” element that exists in a list. So, outcome comes as “element exist”.

Element Exists

Using any()

The any() function takes an iterable ( list, string, dictionary, etc.) in python. The any() function returns a Boolean value – true, if; at least one element of an iterable is true and false if all elements are false or if an iterable is empty.

Example

In the following program, we have used “any()” to finding an element in the given list of 6 strings.

lst=["kiran","arun","varun","kunnal","tiya","rhea" ]
result = any(item in lst for item in lst)
print("Does string contain any list element: " +str(bool(result)))

Output

On executing the above program, following output is generated and the method returns True, because the list contains atleast one element in it.

Does string contain any list element: True

Using find()

find() method is used to find the position of a given substring in a string, if it is given in the list. It helps us to find the index of the first occurrence of substring. It returns -1 substring is not present in the given string.

Example

Here, we have a program in which we have used “find()” in finding an element.

lst=["kiran","arun","varun","kunnal","tiya","rhea" ]
print("Checking if kunnal exists in list")
x=list(map(str,lst))
y="-".join(x)
if y.find("kunnal") !=-1:
   print("Yes, kunnal exists in list")
else:
   print("No, kunnal does not exists in list")

Output

In this program we have to search “kunnal” element that exists in a list. So, outcome comes as “yes, kunnal exist in list”, as shown below, when executed.

Checking if kunnal exists in list
Yes, kunnal exists in list

Using Counter()

A counter() is a dict subclass for counting hashable objects. It is a collection where elements can be stored as dictionary value. It can also the negative and zero values. For example −

Example

Here, we have a program in which we have used “counter()” in finding an element

from collections import Counter
lst=["kiran","arun","varun","kunnal","tiya","rhea" ]
frequency = Counter(lst)
if(frequency["kunnal"] > 0):
   print("Yes, kunnal exists in list")
else:
   print("No, kunnal does not exists in list")

Output

Following output is generated upon executing the above program, the program searches for “kunnal” element, which exists in the list. So, outcome comes as follows -

Yes, kunnal exists in list

Using Count()

Count() function is used as in-built python List method, count(), to check if the passed element exists in the List. If the passed element exists in the List, the 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.

Example

we have a program in which we used “count()”to finding an element

lst=["kiran","arun","varun","kunnal","tiya","rhea" ]
print("Checking if kunnal exists in list")
exist_count = lst.count("kunnal")
if exist_count> 0:
   print("Yes, kunnal exists in list")
else:
   print("No, kunnal does not exists in list")

Output

Checking if kunnal exists in list
Yes, kunnal exists in list

Conclusion

In this article, we have briefly discussed about the different methods used to search an element from the list.

Updated on: 21-Apr-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements