Group List Elements | Challenge

PythonPythonBeginner
Practice Now

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

Introduction

In Python, we can group the elements of a list based on a given function. This can be useful in many situations, such as when we want to group a list of words by their length or a list of numbers by their parity. In this challenge, you will be asked to write a function that groups the elements of a list based on a given function.


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(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") 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`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13117{{"`Group List Elements | Challenge`"}} python/variables_data_types -.-> lab-13117{{"`Group List Elements | Challenge`"}} python/for_loops -.-> lab-13117{{"`Group List Elements | Challenge`"}} python/lists -.-> lab-13117{{"`Group List Elements | Challenge`"}} python/tuples -.-> lab-13117{{"`Group List Elements | Challenge`"}} python/dictionaries -.-> lab-13117{{"`Group List Elements | Challenge`"}} python/function_definition -.-> lab-13117{{"`Group List Elements | Challenge`"}} python/importing_modules -.-> lab-13117{{"`Group List Elements | Challenge`"}} python/using_packages -.-> lab-13117{{"`Group List Elements | Challenge`"}} python/standard_libraries -.-> lab-13117{{"`Group List Elements | Challenge`"}} python/math_random -.-> lab-13117{{"`Group List Elements | Challenge`"}} python/data_collections -.-> lab-13117{{"`Group List Elements | Challenge`"}} python/build_in_functions -.-> lab-13117{{"`Group List Elements | Challenge`"}} end

Group List Elements

Problem

Write a function group_by(lst, fn) that takes a list lst and a function fn as arguments and returns a dictionary where the keys are the results of applying fn to the elements of lst and the values are lists of elements from lst that produce the corresponding key when fn is applied to them.

For example, if we have a list of numbers [6.1, 4.2, 6.3] and we want to group them by their integer part, we can use the floor function from the math module as the grouping function. The expected output would be {4: [4.2], 6: [6.1, 6.3]}.

Example

from math import floor

group_by([6.1, 4.2, 6.3], floor) ## {4: [4.2], 6: [6.1, 6.3]}
group_by(['one', 'two', 'three'], len) ## {3: ['one', 'two'], 5: ['three']}

Summary

In this challenge, you have learned how to group the elements of a list based on a given function. You have also written a function that does this by using a dictionary to store the groups and a loop to populate them. This is a useful technique that can be applied in many different contexts.

Other Python Tutorials you may like