Curried List Element Randomizer

  • Share this:

Code introduction


This function is a higher-order function that uses `django.utils.functional.curry` to create a function that takes a single element as a parameter. This function accepts a list, and if an element in the list is also a list, it will randomly select an element from it; otherwise, it will return the element directly.


Technology Stack : Django, random, curry

Code Type : Django Utility Function

Code Difficulty : Intermediate


                
                    
def randomize_list_element(lst):
    import random
    from django.utils.functional import curry
    
    def wrap_randomize_list_element(element):
        if isinstance(element, list):
            return [random.choice(e) if isinstance(e, list) else e for e in element]
        return element
    
    return curry(wrap_randomize_list_element)