 # How to Search for a Number in a List in Python?

Python is a versatile programming language that offers a wide range of functionalities to its users. One of the most common tasks in programming is searching for a specific element in a list. In this tutorial, we will explore how to search for a number in a list in Python. We will cover different methods to achieve this task. By the end of this tutorial, you will have a clear understanding of how to search for a number in a list in Python and which method to use depending on the specific use case.

## What does it mean to search for a number in a list?

Well depending upon the use case, it could mean different things, you may ask yourself the following questions to determine what is it that you’re looking for –

1. Do you only want to know whether the number is present in the list or not?
2. Do you want the know the frequency of occurrence of a number in a list?
3. Do you want to know the index of the number in the list?
4. If there are multiple occurrences of the number in a list, do you want to know the index of a specific occurrence or all the occurrences?

Depending upon the answer to the above questions you may use one or more of the methods mentioned below.

## Methods to search for a number in a list in Python?

Let’s look at some methods that you can use to search for a number (or an element) in a list. Note that all these methods may give different results depending on what the use case is.

### 1) Using the `in` operator

The membership operator in Python, `in` is used to check whether an object (for example, an element) is part of a collection (for example, a list) or not. It gives a boolean result.

You can use the `in` operator to check whether a number is present in a list or not. Let’s look at an example.

```# create a list
ls = [1, 2, 3, 4, 5]
# search for 3 in ls
print(3 in ls)
# search for 7 in ls
print(7 in ls)```

Output:

```True
False```

Here, we used the `in` operator to check whether the numbers 3 and 7 are present in the list `ls` or not. You can see that we get `True` for 3 and `False` for 7, which is the correct result.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

### 2) Using the `count()` function

The built-in list `count()` function in Python is used to count the occurrences of an element in a list.

• If the count is greater than 0 you can say that the element is present in the list.
• If the count is equal to 1, you can say that the element occurs only once in the list.
• If the count is greater than 1, you can say the element occurs more than once.

Let’s look at an example.

```# create a list
ls = [1, 2, 3, 4, 5, 3]
# get count of 7 in ls
print("The count of the number 7 in the list is", ls.count(7))
# get count of 7 in ls
print("The count of the number 5 in the list is", ls.count(5))
# get count of 3 in ls
print("The count of the number 3 in the list is", ls.count(3))```

Output:

```The count of the number 7 in the list is 0
The count of the number 5 in the list is 1
The count of the number 3 in the list is 2```

### 3) Using the `index()` function

The built-in list `index()` function in Python is used to find the index of the first occurrence of an element in a list. It takes an element as an argument and returns the index of the first occurrence of that element in the list. If the element is not found in the list, it raises a `ValueError`.

Using the `index()` function you can search for a number in a list and get the index of its first occurrence. Let’s look at an example.

```# create a list
ls = [1, 2, 3, 4, 5, 3]
# get the index of 3 in ls
print(ls.index(3))```

Output:

`2`

In the above list, the number 3 occurs twice (at indexes 2 and 5), you can see that we get 2 as the result of the index function which is the index of the first occurrence of 3.

Now, let’s see an example of finding the index of a number that is not present in the list.

```# create a list
ls = [1, 2, 3, 4, 5, 3]
# get the index of 7 in ls
print(ls.index(7))```

Output:

```---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

Cell In, line 4
2 ls = [1, 2, 3, 4, 5, 3]
3 # get the index of 7 in ls
----> 4 print(ls.index(7))

ValueError: 7 is not in list```

We get a `ValueError` stating that 7 is not present in the list. Now, we can use error handling to communicate that the number is not present without raising an error in a more clean way.

```# create a list
ls = [1, 2, 3, 4, 5, 3]
# get the index of 7 in ls
try:
print(ls.index(7))
except ValueError:
print("The number is not present in the list")```

Output:

`The number is not present in the list`

### 4) Iterating through the list

What if you want to know all the indexes of occurrences of a number in a list? You can do so by iterating through each value in the list and keeping track of the indexes at which your target number appears.

Let’s look at an example.

```# create a list
ls = [1, 2, 3, 4, 5, 3]

# get all indexes of 3 in ls
target = 3
index_ls = []
for index, val in enumerate(ls):
if val == target:
index_ls.append(index)

# display the indexes
print(index_ls)```

Output:

`[2, 5]`

Here, we use the `enumerate()` function to loop through `ls` and get the index and value of each item in the list. In each iteration, we check whether the value is equal to our target value or not, if it is, we add its index to the `index_ls` list.

You can see that we get all the indexes of 3 in the list.

With the values in the `index_ls` list you can answer the following questions.

```print("Is 3 present in the list?", len(index_ls) > 0)
print("What is the index of first occurrence of 3 in the list?", index_ls)
print("What is the index of last occurrence of 3 in the list?", index_ls[-1])```

Output:

```Is 3 present in the list? True
What is the index of first occurrence of 3 in the list? 2
What is the index of last occurrence of 3 in the list? 5```

## Conclusion

In conclusion, Python provides several methods to search for a number in a list. We can use the built-in `in` operator to check if an element exists in a list or use the `index()` method to get the index of the first occurrence of an element in a list. We can also use the `count()` method to count the number of occurrences of an element in a list. Additionally, we can use the `enumerate()` function to get the index and value of each element in a list and then check for the desired number. It is important to choose the appropriate method based on the specific requirements of the program. By using these methods, we can efficiently search for a number in a list in Python.

You might also be interested in –

## Author

• Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top