Find Parity Outliers | Challenge

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") subgraph Lab Skills python/comments -.-> lab-13107{{"`Find Parity Outliers | Challenge`"}} python/conditional_statements -.-> lab-13107{{"`Find Parity Outliers | Challenge`"}} python/for_loops -.-> lab-13107{{"`Find Parity Outliers | Challenge`"}} python/list_comprehensions -.-> lab-13107{{"`Find Parity Outliers | Challenge`"}} python/lists -.-> lab-13107{{"`Find Parity Outliers | Challenge`"}} python/tuples -.-> lab-13107{{"`Find Parity Outliers | Challenge`"}} python/function_definition -.-> lab-13107{{"`Find Parity Outliers | Challenge`"}} python/importing_modules -.-> lab-13107{{"`Find Parity Outliers | Challenge`"}} python/using_packages -.-> lab-13107{{"`Find Parity Outliers | Challenge`"}} python/standard_libraries -.-> lab-13107{{"`Find Parity Outliers | Challenge`"}} end

Find Parity Outliers

Problem

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:

  1. Use collections.Counter with a list comprehension to count even and odd values in the list.
  2. Use collections.Counter.most_common() to get the most common parity.
  3. Use a list comprehension to find all elements that do not match the most common parity.

Example

find_parity_outliers([1, 2, 3, 4, 6]) ## [1, 3]

In the example above, the majority of the elements in the list are even, so the parity outliers are the odd elements 1 and 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.

Other Python Tutorials you may like