July 02, 2022

Filter data in python with high order functions

This project (Platzi) is to learn How to filter data from a list of dictionaries. The goal is to use the python high order functions filter() and map() to achieve the task. Note: You will require python > v3.9 to finish the 100% of the examples. However if you have only python 3 you can achieve the 90%. Don’t worry, let’s practice.

The filter( ) , map ( ), and reduce ( ) functions simplifies the process of working with lists and dictionaries.

List of dictionaries

This is the list of dictionaries provided by Platzi where we will perform some filter tasks. Take a look to the name of the dictionary DATA is capitalized, this means the list of dictionary will be a constant along the program.

DATA = [
    {
        'name': 'Facundo',
        'age': 72,
        'organization': 'Platzi',
        'position': 'Technical Coach',
        'language': 'python',
    },
    {
        'name': 'Luisana',
        'age': 33,
        'organization': 'Globant',
        'position': 'UX Designer',
        'language': 'javascript',
    },
    {
        'name': 'Héctor',
        'age': 19,
        'organization': 'Platzi',
        'position': 'Associate',
        'language': 'ruby',
    },
    {
        'name': 'Gabriel',
        'age': 20,
        'organization': 'Platzi',
        'position': 'Associate',
        'language': 'javascript',
    },
    {
        'name': 'Isabella',
        'age': 30,
        'organization': 'Platzi',
        'position': 'QA Manager',
        'language': 'java',
    },
    {
        'name': 'Karo',
        'age': 23,
        'organization': 'Everis',
        'position': 'Backend Developer',
        'language': 'python',
    },
    {
        'name': 'Ariel',
        'age': 32,
        'organization': 'Rappi',
        'position': 'Support',
        'language': '',
    },
    {
        'name': 'Juan',
        'age': 17,
        'organization': '',
        'position': 'Student',
        'language': 'go',
    },
    {
        'name': 'Pablo',
        'age': 32,
        'organization': 'Master',
        'position': 'Human Resources Manager',
        'language': 'python',
    },
    {
        'name': 'Lorena',
        'age': 56,
        'organization': 'Python Organization',
        'position': 'Language Maker',
        'language': 'python',
    },
]

Filter the python programmers

Goal: Filter only the python programmers from the DATAlist using list comprehensions

Method 1: Use list comprehensions

def run():
    all_python_devs = [worker['name'] for worker in DATA if worker['language'] == 'python']
    for worker in all_python_devs:
        print(worker)

if __name__ == '__main__':
    run()

Filter all the adults (age > 18 year)

Goal: Filter all the adults workers from the DATAlist using filter and print their names

  • Filter is a high order function in Python : filter and map
  • Use lambda functions

Method 1: Use filter (high order function)

Using the filter()function requires lambda worker: worker['age'] > 18 ,DATA to extract the adults workers. However, doing the following code we are extracting all the dictionary from DATA. Therefore we will require the map()function to select only the names of the adult workers.

def run():
  
    adults = list(filter(lambda worker: worker['age'] > 18 ,DATA))

    for worker in adults:
        print(worker)

if __name__ == '__main__':
    run()

Method 2: User filter() + map() (high order functions)

To achieve the goal we need to pass the filter() and map() functions to extract first all the dictionaries with the adult workers and second to extract the names of the adult workers.

def run():
  
    adults = list(filter(lambda worker: worker['age'] > 18 ,DATA))
    adults = list(map(lambda worker: worker['name'], DATA))

    for worker in adults:
        print(worker)

if __name__ == '__main__':
    run()

Filter old people (age > 70 )

Goal:

  • Create a new dictionary old_people with workers (age > 70 ) , but include the ‘old’:True or ‘old’: False into each worker dictionary.

Method 1: Use map()

When we apply the map() function by filtering with worker['age'] > 70we will have a list()with True and False value. Thus, we need to create the new dictionary

def run():

    old_people = (list(map(lambda worker: worker['age'] > 70, DATA)))

    for worker in old_people:
        print(worker)



if __name__ == '__main__':
    run()

Method 2: Use map() + sum of dictionaries

To add a new entry to the dictionary we will use the | command

Note: This feature only works with python > 3.9

def run():
  
    old_people = list(map(lambda worker: worker | {'old' : worker['age'] > 70 }, DATA))

    for worker in old_people:
        print(worker)



if __name__ == '__main__':
    run()

Extra: Look about high order functions in YouTube

This is an extra step, don’t jump this pass. Every time you learn a new concept practice some examples but remember if you want to secure your learning. You must integrate it to you workflow. The high order functions will simplify your list tasks for filtering data.

If you love this content, you can share a Cup of Coffee ! Your help is essential to continue with this project