Numbers that appear exactly once
EasyList Programs · 36 of 100
Write a Python program to get the numbers that appear exactly once in a list.
Input[1, 2, 2, 3, 4, 4, 5]
Output[1, 3, 5]
[1, 2, 3, 4, 5]
Approach
- Count with
Counter and keep counts of 1.
Solution
Python
from collections import Counter numbers = [1, 2, 2, 3, 4, 4, 5]counts = Counter(numbers)print([n for n in numbers if counts[n] == 1])print(list(dict.fromkeys(numbers)))
Output
[1, 3, 5]
[1, 2, 3, 4, 5]
Line by line
Line 5Values that occur only once.
Line 6Every distinct value (duplicates removed), for comparison.
Learn the concept
New to this topic? Read the lesson, then come back.
More list programs
← All 100 List Programs