Find Missing Number in List in Python (2 Examples)
Hi! This tutorial will explain how to get the missing number in a list in the Python programming language.
Here is an overview:
Let’s jump into the Python code!
Create Sample List of Integers
So, in your favorite Python IDE, run the code below to create the sample list of integers:
nums = [1, 2, 3, 5, 6, 7] print(nums) # [1, 2, 3, 5, 6, 7] print(type(nums)) # <class 'list'>
Now that we have created the sample list of integers, we can now examine two ways to determine which number is missing from the list.
Example 1: Get Number Missing in List Using for Loop & Conditional Statement
for i in range(1,len(nums)+1): if i not in nums: print(i) # 4
In the example above, in the range() function, we started the range from 1 instead of 0, because the integers in the list start from 1.
Then, we added 1 to the length of the list, because the last number in the range, by default, is 6, whereas the last number in the list is 7. Adding 1 to the length of the list makes the range uniform with the elements in the list.
Next, we used a conditional “if” statement and the not in operators to detect which number in the for loop iteration is not in the list. If the number in the iteration is not in the list, then it gets printed out. In this case, the number that is not in the list is the number 4.
Example 2: Get Number Missing in List Using NumPy
For this example, we will need to install and import Python’s NumPy library, which is the foremost library in Python for numerical and scientific computations.
Run the lines of code below to install and import NumPy:
# install NumPy pip install numpy # import NumPy import numpy as np
With NumPy installed and imported into our Python environment, let us now use its functions to find the missing number in the list:
n = len(nums) + 1 expected_nums = np.arange(1, n) missing_nums = np.setdiff1d(expected_nums, nums) print(missing_nums) #4
Here, we created a variable “n”, which is the length of the list plus 1. We added 1 to the length of the list because we are looking for a missing number in a sequence of numbers from 1 to n.
Next, we used NumPy’s np.arrange() function to calculate the expected list of numbers, and it generates an array of numbers from 1 to n.
Then, we used np.setdiff1d() function to find the difference between “expected_nums” and “nums”. This returns an array of numbers that are in “expected_nums” but not in “nums”. This array is then stored in a variable called “missing_nums”.
Finally, we return the first element of “missing_nums” using the index .
So, with that, we have demonstrated with 2 examples how to find the missing number in a list in Python. I do hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on how to find the missing number in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to find the missing number in a list in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- count() Method for Lists in Python (2 Examples)
- Convert List from Boolean to String in Python (2 Examples)
- Learn Python Programming
- Append to 2D List in Python (3 Examples)
- Transpose 2D List in Python (3 Examples)
This post has shown how to find the missing number in a list in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.