How to find duplicate and missing values in a sorted range (x to y) list with Python

2 Answers

0 votes
from functools import reduce

x = 4;
y = 15;
    
lst = [5, 5, 5, 5, 6, 7, 9, 10, 10, 10, 11, 13]

missingValues = set(range(x, y + 1)).difference(lst)     

duplicatesValues = reduce(lambda i, j: i + j, [[n] * (lst.count(n) - 1) 
                          for n in set(lst) if lst.count(n) > 1], [])

print("missingValues:", missingValues)
print("duplicatesValues:", duplicatesValues)


'''
run:
   
missingValues: {4, 8, 12, 14, 15}
duplicatesValues: [5, 5, 5, 10, 10]

'''

 



answered Oct 25, 2024 by avibootz
0 votes
import collections

x = 4;
y = 15;
    
lst = [5, 5, 5, 5, 6, 7, 9, 10, 10, 10, 11, 13]

missingValues = set(range(x, y + 1)).difference(lst)     

duplicatesValues = [item for item, count in collections.Counter(lst).items() if count > 1]

print("missingValues:", missingValues)
print("duplicatesValues:", duplicatesValues)



'''
run:
   
missingValues: {4, 8, 12, 14, 15}
duplicatesValues: [5, 10]

'''

 



answered Oct 25, 2024 by avibootz
...