w3resource logo


Python Exercises

Python Challenges: Find missing numbers from a list

Python Challenges - 1: Exercise-8 with Solution

Write a Python program to find missing numbers from a list.

Input : [1,2,3,4,6,7,10]
Output : [5, 8, 9]

Explanation :

Python: Find a missing numbers from a list

Sample Solution :-

Python Code :

def missing_numbers(num_list):
      original_list = [x for x in range(num_list[0], num_list[-1] + 1)]
      num_list = set(num_list)
      return (list(num_list ^ set(original_list)))

print(missing_numbers([1,2,3,4,6,7,10]))
print(missing_numbers([10,11,12,14,17]))

Output :

[5, 8, 9]                                                               
[13, 15, 16]

Flowchart:

Python Flowchart: Find a missing numbers from a list

Python Code Editor:

def missing_numbers(num_list):
      original_list = [x for x in range(num_list[0], num_list[-1] + 1)]
      num_list = set(num_list)
      return (list(num_list ^ set(original_list)))

print(missing_numbers([1,2,3,4,6,7,10]))
print(missing_numbers([10,11,12,14,17]))

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Python: How to check whether a file exists using Python?

C#: Loops in c#

PHP: For each

SQL: JOIN using more than 5 tables

JavaScript: Blank Slide in Javascript Slideshow