Toolz Module in Python

In this tutorial, we will learn about the toolz module, its function and how we can it solve complex programming language.

The Toolz package offers many functions to work with lists, functions, and dictionaries. It adds more features to the library provided by the regular Python libraries itertools and functools. Toolz module helps you work with your code and data more effectively.

This module contains the following five other modules -

  • Dicttoolz
  • Functools
  • Itertools
  • Recipes
  • Sandbox

Let's understand about these modules -

Dicttoolz

The dicttoolz is a module within the Toolz package that specifically focuses on providing utility functions for working with dictionaries in Python. It offers various helpful functions that allow you to manipulate and interact with dictionaries in a more efficient and convenient way. These functions extend the capabilities of the built-in dictionary operations and offer additional features to make working with dictionaries easier.

Let's understand the following functions -

  • assoc(d, key, value[, factory]) - It Creates a new dictionary by adding a new key-value pair. The original dictionary remains unchanged.

Example:

Output:

Original Dictionary: {'Java': 0}
New Dictionary: {'Java': 0, 'Tpoint': 1}
  • assoc_in(d, keys, value[, factory]) - It generates and returns a new dictionary by adding a new key-value pair, which can be nested under specific keys. The original dictionary remains unaltered.

Example:

Output:

Original Dictionary: {'Java': 0}
New Dictionary: {'Java': 0, 'Tpoint': 1}
  • dissoc(d, *keys) - It creates a new dictionary by removing the specified key(s) from the dictionary. The original dictionary remains unchanged.

Example:

Output:

{'P': 0, 't': 2, 'r':4}
  • get_in(keys, ds[, default, no_default]) - It retrieves the value from a nested dictionary `ds` using a sequence of keys `[I0, I1, …, IX]`. If the value cannot be found, it returns the specified `default`.

Example:

Output:

v2
v4
  • get_in(keys, ds[, default, no_default]) - It retrieves the value from a nested dictionary `ds` using a sequence of keys [I0, I1, …, IX]`. If the value cannot be found, it returns the specified `default`.

Example:

Output:

v2
v4
  • itemmap(func, d[, factory]) - It applies the specified function to each item in the dictionary. It creates a new dictionary with the result of applying the function to each item.

Example:

Output:

{'A': 0, 'B': 1, 'C': 3, 'F': 5}
  • keyfilter(predicate, d[, factory])- It filters dictionary items based on the specified predicate function applied to keys.

Example:

Output:

{'': 0, 'julia': 1}
  • keymap(func, d[, factory]) - It applies the specified function to each key in the dictionary. Let's understand the following example.

Example -

Output:

{'': 0, 'JULIA': 1, 'JAVA': 3, 'JAVASCRIPT': 5}
  • merge(*dicts, kwargs) - It merges a collection of dictionaries. Let's understand the following example.

Example -

Output:

{1: 1, 2: 8, 3: 9, 4: 16}
  • merge_with(func, *dicts, kwargs) - It merges dictionaries and applies a function to combined values. Let's understand the following example.

Example -

Output:

{1: 2, 2: 12, 3: 9}
  • update_in(d, keys, func[, default, factory]) - It updates value in a nested dictionary. Let's understand the following example.

Example -

Output:

{1: {11: 55}, 2: {22: 222}}
  • valfilter(predicate, d[, factory]) - It filters items in dictionary by value. Let's understand the following example.

Example -

Output:

{0: '', 1: 'julia'}
  • valmap(func, d[, factory]) - It applies function to values of dictionary. Let's understand the following example.

Example -

Output:

{0: '', 1: 'JULIA', 3: 'JAVA', 5: 'JAVASCRIPT'}

Functoolz

The functoolz is a Python library that provides a collection of utility functions for working with functions and callable objects. It extends the functionality of the built-in functools module by providing additional functions that are useful in functional programming and data manipulation tasks.

The toolz extends the capabilities of standard libraries like itertools and functools, functoolz extends the built-in functools module by adding more functionality that is common in functional programming and data processing.

The library includes various functions that help in composing, currying, and manipulating functions, among other things. It provides a set of tools to work with functions in a more functional and concise manner, allowing for cleaner and more expressive code.

Functions

  • apply(*func_and_args, **kwargs) - This function simply applies a given function with the provided arguments and returns the result.

Example -

Output:

25
  • complement(func) - This function returns the logical complement of the provided function's output. Let's understand the following example -

Example -

Output:

False
True

Explanation -

In the above example, the complement() function is used to create a new function `not_divisible_by_3`, which returns the opposite of `is_divisible_by_3()`. This allows you to easily toggle the outcome of the function based on your needs.

  • compose(*funcs) - This function creates a new function that applies a sequence of functions in a specific order, from right to left. If no functions are provided, it returns the identity function (which simply returns its input unchanged).

Example -

Output:

18

Explanation -

In the example, the compose() function combines the double() and square() functions to create a new function. When this composed function is applied to the value 3, it first doubles it to 6 and then squares it to get the final result of 18.

  • compose_left(*funcs) - This function creates a new function that applies a sequence of functions in a specific order, from left to right. If no functions are provided, it returns the identity function (which simply returns its input unchanged).

Example -

Output:

18

Explanation -

In the example, the compose_left() function combines the double() and square() functions to create a new function. When this composed function is applied to the value 3, it first squares it to 9 and then doubles it to get the final result of 18.

  • flip(oper, a, b) - This function reverses the order of arguments when calling a function.

Example

Output:

2.0

Explanation -

In the example, the flip() function is used to reverse the order of arguments when calling the `divide()` function. Instead of 4 / 8, it becomes 8 / 4, resulting in a quotient of `2.0`.

  • identity(x) - The identity function returns the input value as is.

Example -

Output:

6

The pipe() function in the toolz package allows you to guide a value through a sequence of functions, one after the other. It's akin to applying a sequence of functions from left to right. Let's understand the following example -

Example -

Output:

36

In the given example, the value 3 is first doubled to 6 and then squared to yield the final result of 36 using the pipe() function.

  • pipe() - The pipe() function in the toolz package allows you to guide a value through a sequence of functions, one after the other. It's akin to applying a sequence of functions from left to right.

Example -

Output:

36

In the given example, the value 3 is first doubled to 6 and then squared to yield the final result of 36 using the pipe() function.

  • thread_last() - The thread_last() function from the `toolz` package allows you to sequentially apply a set of functions or forms to a given value. It operates by passing the value through these functions/forms from left to right, similar to threading a string through the eye of a needle.

Example -

Output:

4

Explanation -

In this provided example, the value 3 is first applied to the `mod` function with the argument 2, producing the output 1. Next, the output 1 is doubled using the double() function, resulting in the final output of `4` through the thread_last().

Itertoolz

The itertoolz is a Python library that provides a set of utility functions for working with iterators and iterable objects. It is designed to extend and enhance the capabilities of the built-in `itertools` module, providing additional functions that is useful for common data manipulation tasks. The library is part of the `toolz` ecosystem, which aims to provide various utilities for functional programming and data manipulation in Python.

It offers functions that allow you to efficiently perform operations on iterables such as lists, tuples, generators, and more. These functions enable you to compose complex data transformations and manipulations using a functional programming paradigm, making your code more readable and maintainable.

The library includes functions for common tasks such as filtering, mapping, grouping, and transforming data within iterables. By using itertoolz, you can write code that follows functional programming principles, which can lead to more modular code.

Functions

  • concat() - The concat(seqs) function allows to combine multiple sequences (such as lists) into one by one after. It's like creating a longer sequence that contains all the elements from the original sequences in the order you provide. Let's understand the following example.

Example -

Output:

[1, 'a', 2, 3, 4]

In this example, the concat() function takes the three lists and creates a new list with all their elements, maintaining their original order. The resulting list `[1, 'a', 2, 3, 4]` includes elements from all three input lists.

  • diff() - The `diff(*seqs, **kwargs)` function allows you to compare elements located at corresponding positions in two iterables. Subsequently, it generates a list containing pairs of elements that differ between the two sequences.

Let's understand the following example -

Example -

Output:

[(1, 2), (3, 4)]

In this example, the diff() function inspects the elements at the same indices in the sequences `[1, 2, 3]` and `[2, 2, 4]`. It subsequently assembles pairs, such as `(1, 2)` and `(3, 4)`, to signify the differing elements at these corresponding positions.

  • drop() - The `drop(n, seq)` function from the `itertoolz` library allows you to exclude the initial `n` elements from a sequence. The modified sequence, excluding those initial elements, is then returned. Let's understand the following example -

Example -

Output:

[6, 4, 7]

In this example, the `drop` function omits the first three elements from the provided sequence [2, 3, 2, 6, 4, 7], resulting in the modified sequence [6, 4, 7].

  • frequencies(seq) - The `frequencies(seq)` function in the itertoolz library generates a dictionary that shows the occurrences of elements along with their counts in the given sequence. This operation is comparable to using collections.Counter. Let's understand the following example.

Example -

Output:

{'c': 2, 'b': 3, 'd': 1, 'e': 1, 'h': 2}

In this example, the frequencies() function examines the elements in the provided sequence and constructs a dictionary that displays the occurrences and counts of each element.

  • groupby(func, seq) - The groupby(func, seq) function forms a dictionary by grouping the elements of the sequence based on the results of applying the provided function. Let's understand the following example.

Example -

Output:

{5: ['geeks', 'geeks'], 3: ['for']}

In this example, the groupby() function applies the `len` function to each element of the sequence. It then forms a dictionary where the keys represent the lengths of elements and the corresponding values are lists containing the elements of that length.

  • groupby(func, seq) - The `groupby(func, seq)` function forms a dictionary by grouping the elements of the sequence based on the results of applying the provided function. Let's understand the following example -

Example -

Output:

{5: ['geeks', 'geeks'], 3: ['for']}
  • isiterable(x) - The function `isiterable(x)` from the `itertoolz` library determines whether a given object `x` is iterable or not. If the object can be iterated over, it returns `True`; otherwise, it returns `False`.

Example:

Output:

True

In this example, the function checks whether the list my_list is iterable. Since lists are iterable objects, the function returns `True`.

  • interleave(seqs) - The function interleave(seqs) combines sequences by interleaving their elements based on their indices. It concatenates elements from each sequence at the same index position.

Example:

Output:

[10, 5, 20, 8, 11]

In this example, the function interleaves the elements of the sequences `[10, 20]` and `[5, 8, 11]`, resulting in `[10, 5, 20, 8, 11]`.

  • topk(k, seq[, key]) - This function selects the k largest elements from a sequence. It's useful for identifying the highest values in a collection.

Example -

Output:

[95, 92, 90]

In this instance, the topk() function extracts the three highest scores from the list [95, 85, 75, 90, 88, 92], resulting in the output [95, 92, 90].

unique(seq[, key]) - This function retrieves and returns the unique elements from a sequence, similar to how the set() function operates. Let's understand the following example.

Example -

Output:

['red', 'blue', 'green', 'yellow']

In this example, the `unique` function filters out the duplicate elements from the list `['red', 'blue', 'green', 'blue', 'yellow', 'red']`, resulting in the output `['red', 'blue', 'green', 'yellow']`.

  • merge_sorted(*seqs, **kwargs) - This function merges multiple sorted iterables into a single sorted collection.

Example -

Output:

[1, 3, 4, 6, 8, 9, 10, 13, 15, 17, 18, 22]

In this example, the `merge_sorted` function combines the sorted lists `list1`, `list2`, and `list3` to create a single sorted list merged_sorted(), containing all the elements in ascending order.

  • mapcat(func, seqs) - This function applies the specified function to each sequence and then concatenates the results into a single sequence.

Example -

Output:

[6, 14, 20, 10, 16, 24]

In this example, the `mapcat` function applies the double_elements() function to each of the input lists `list1` and `list2`. The results are then concatenated together into the final `result` list.

remove(predicate, seq) - This function returns the elements from the sequence for which the specified predicate function returns False. It can be thought of as the complement function of the `filter` function.

Example -

Output:

[9, 15, 7]

In this example, the `remove` function applies the `is_even` predicate function to each element in the `numbers` list. Elements for which the predicate returns False are included in the final `result` list.

Recipes

The recipes refer to a collection of commonly used functions and workflows that provide efficient and concise solutions to various programming tasks. These recipes are designed to help you work with iterators, sequences, and data processing in a more streamlined and functional way. They leverage the functions and utilities provided by the toolz module to achieve specific goals.

  • countby() - The countby() function allows you to count elements of a collection based on a specified key function. This function is particularly useful for categorizing and counting elements based on specific criteria. It enables you to categorize and count elements in a collection using a chosen key function. This can be beneficial for organizing data according to specific attributes.

Example -

Output:

{True: 3, False: 3}

Explanation -

In this example, the `countby` function categorizes the numbers in the list based on whether they are prime or not. The resulting dictionary indicates that there are 3 prime numbers and 3 non-prime numbers in the given list.

  • partitionby() - The partitionby function facilitates the segmentation of a sequence based on a specified function. It groups consecutive elements together if they satisfy the condition set by the function.

It allows you to divide a sequence into subgroups based on a chosen function. It clusters consecutive elements that fulfill the given function's criteria. Let's understand the following example.

Example -

Output:

[(3,), (8, 11), (20,), (23,), (30,), (37,)]

Explanation -

In this example, the partitionby() function segments the list of numbers into groups where consecutive numbers share the same prime status. The resulting list of tuples displays the numbers within each group, based on whether they are prime or not.

Sandbox

The sandbox is used in a context or environment where you can experiment with various functions, utilities, and recipes provided by the toolz library. This concept aligns with the functional programming paradigm that toolz encourages.

Functions -

  • fold(binop, seq[, default, map, ...]) - This function performs reduction on a sequence using a binary operation, but without preserving the order of reduction. It's suitable for scenarios where order doesn't matter. For example, you can use it to sum up a list of numbers concurrently.

Example -

Output:

24
  • unzip(seq): This function is the reverse of the `zip` operation. It takes a sequence of tuples and returns two separate sequences, effectively splitting the tuples into their individual components.

Example -

Output:

(10, 20, 15)
('apple', 'banana', 'orange')

Conclusion

The toolz module is a powerful and versatile module that extends the functionality of Python's built-in tools. It provides a collection of functions and recipes designed to simplify common programming tasks, such as working with sequences, dictionaries, and functional programming constructs. The module is organized into several sub-modules, each addressing different aspects of data manipulation and computation.






Latest Courses