Filter Unique List Values

PythonPythonBeginner
Practice Now

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

Introduction

In Python, a list is a collection of items that are ordered and changeable. Sometimes, we need to filter out the unique values from a list. In this challenge, you are required to write a Python function that takes a list as an argument and returns a new list with only the non-unique values.


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-13632{{"`Filter Unique List Values`"}} python/conditional_statements -.-> lab-13632{{"`Filter Unique List Values`"}} python/for_loops -.-> lab-13632{{"`Filter Unique List Values`"}} python/list_comprehensions -.-> lab-13632{{"`Filter Unique List Values`"}} python/lists -.-> lab-13632{{"`Filter Unique List Values`"}} python/tuples -.-> lab-13632{{"`Filter Unique List Values`"}} python/function_definition -.-> lab-13632{{"`Filter Unique List Values`"}} python/importing_modules -.-> lab-13632{{"`Filter Unique List Values`"}} python/using_packages -.-> lab-13632{{"`Filter Unique List Values`"}} python/standard_libraries -.-> lab-13632{{"`Filter Unique List Values`"}} end

Filter Unique List Values

Write a Python function called filter_unique(lst) that takes a list as an argument and returns a new list with only the non-unique values. To solve this problem, you can follow these steps:

  1. Use collections.Counter to get the count of each value in the list.
  2. Use a list comprehension to create a list containing only the non-unique values.

Your function should satisfy the following requirements:

  • The function should take a list as an argument.
  • The function should return a new list with only the non-unique values.
  • The function should not modify the original list.
  • The function should be case-sensitive, meaning that 'a' and 'A' are considered different values.
def filter_unique(lst):
    ## your code here
from collections import Counter

def filter_unique(lst):
  return [item for item, count in Counter(lst).items() if count > 1]
filter_unique([1, 2, 2, 3, 4, 4, 5]) ## [2, 4]

Summary

In this challenge, you have learned how to filter out the unique values from a list using Python. You have also learned how to use collections.Counter to count the occurrences of each value in a list and how to use a list comprehension to create a new list with only the non-unique values.

Other Python Tutorials you may like