
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find missing numbers in a sorted list range in Python
Given a list with sorted numbers, we want to find out which numbers are missing from the given range of numbers.
With range
we can design a for loop to check for the range of numbers and use an if condition with the not in operator to check for the missing elements.
Example
listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range res = [x for x in range(listA[0], listA[-1]+1) if x not in listA] # Result print("Missing elements from the list : \n" ,res)
Output
Running the above code gives us the following result −
Given list : [1, 5, 6, 7, 11, 14] Missing elements from the list : [2, 3, 4, 8, 9, 10, 12, 13]
with ZIP
The ZIP function
Example
listA = [1,5,6, 7,11,14] # printing original list print("Given list : ",listA) # using zip res = [] for m,n in zip(listA,listA[1:]): if n - m > 1: for i in range(m+1,n): res.append(i) # Result print("Missing elements from the list : \n" ,res)
Output
Running the above code gives us the following result −
Given list : [1, 5, 6, 7, 11, 14] Missing elements from the list : [2, 3, 4, 8, 9, 10, 12, 13]
- Related Articles
- Find missing element in a sorted array of consecutive numbers in Python
- Program to find missing numbers from two list of numbers in Python
- Find missing element in a sorted array of consecutive numbers in C++
- Find missing elements in List in Python
- Find missing elements of a range in C++
- Program to find first positive missing integer in range in Python
- Create list of numbers with given range in Python
- Python Generate random numbers within a given range and store in a list
- Python – Random range in a List
- Find the only missing number in a sorted array using C++
- Find pairs with given product in a sorted Doubly Linked List in Python
- Find the one missing number in range using C++
- Program to find squared elements list in sorted order in Python
- How to generate a sorted list in Python?
- What are the different ways to find missing numbers in a sorted array without any inbuilt functions using C#?

Advertisements