# Compare Number with List in Python (2 Examples)

In this tutorial, youâ€™ll learn how to compare a number with a list in Python Programming Language. Comparing a number with a list involves checking if the number exists in the list or determining its position within the list. Weâ€™ll explore different methods to achieve this comparison.

Letâ€™s get started!

## Initializing Sample Number and List

Firstly, letâ€™s initialize a sample number and a sample list:

```# Initializing a sample number
my_number = 5

# Initializing a sample list
my_list = [1, 2, 3, 4, 5]```

This creates a number, `my_number`, and a list, `my_list`, containing some elements.

## Example 1: Checking Number Existence in List

One way to compare a number with a list is to check if the number and the list have the same value. Hereâ€™s an example:

```# Checking number existence in the list
print(my_number in my_list)
# True```

In this example, we use the in operator to check if `my_number` exists in `my_list`. The output shows whether the number exists in the list.

## Example 2: Finding Number Position in List

Another way to compare a number with a list is to find the position of the same number value within the list. Hereâ€™s how you can do it:

```# Finding number position in the list
print(my_list.index(my_number))
# 4```

In this example, we use the index() method to find the position of `my_number` within `my_list`. The output shows the position of the number within the list.

## Video, Further Resources & Summary

In this tutorial, we explored different methods to compare a number with a list in Python. We learned how to check the presence of a number in a list and how to find its position within the list. These techniques can be helpful in scenarios where you need to determine the presence or position of a number within a list.

Do you need more explanations on looping through a list of integers in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

For more Python programming tutorials and examples, you can check out the following resources on Statistics Globe:

Now you have the knowledge and techniques to compare a number with a list in Python. Happy coding!

This page was created in collaboration with Ã–mer Ekiz. Have a look at Ã–merâ€™s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.

Subscribe to the Statistics Globe Newsletter