Introduction
In this challenge, you are tasked with finding the parity outliers in a given list. Parity outliers are elements in a list that have a different parity (odd or even) than the majority of the elements in the list.
Find Parity Outliers
Write a function find_parity_outliers(nums) that takes a list of integers nums as an argument and returns a list of all the parity outliers in nums.
To solve this problem, you can follow these steps:
- Use
collections.Counterwith a list comprehension to count even and odd values in the list. - Use
collections.Counter.most_common()to get the most common parity. - Use a list comprehension to find all elements that do not match the most common parity.
from collections import Counter
def find_parity_outliers(nums):
return [
x for x in nums
if x % 2 != Counter([n % 2 for n in nums]).most_common()[0][0]
]
find_parity_outliers([1, 2, 3, 4, 6]) ## [1, 3]
Summary
In this challenge, you learned how to find the parity outliers in a list of integers. You used the collections.Counter module to count even and odd values in the list, and then found the most common parity. Finally, you used a list comprehension to find all elements that do not match the most common parity.