Pydash: A Kitchen Sink of Missing Python Utilities | by Khuyen Tran | Towards Data Science

Have you ever tried to flatten a nested array like this?
If you found it difficult to flatten such a nested array, you would be happy to find an elegant solution like this:
...or to get the object of a deeply nested dictionary-like below in one line of code.
If you are looking for a library that provides useful utilities to deal with Python objects like above, pydash will be your go-to library.
Pydash is the kitchen sink of Python utility libraries for doing “stuff” in a functional way.
To install pydash, make sure your Python version is ≥ 3.6, then type:
pip install pydash
Start with importing pydash:
You can flatten a nested Python list using the py_.flatten method:
What if your list is deeply nested like below?
That is when the py_.flatten_deep method comes in handy:
If you can flatten a list, can you also turn a flattened list into a nested one? Yes, that could be done with the py_.chunk method:
Nice! The elements in the list are split into groups of 2.
To omit an attribute from the dictionary, we can use the py_.omit method:
How do you get the price of an apple from Walmart that is in season in a nested dictionary like below?
Normally, you need to use a lot of brackets to get that information:
[2, 4]
Wouldn’t it be nice if you could use the dot notation instead of brackets? That could be done with the py_.get method:
Cool! You can also get the element in an array using [index] :
2
To get the index of an element in a list using a function, use the py_.find_index method:
The py_.find_index method allows you to get the index of the object that matches a certain pattern. But what if you want to get the items in a list instead of the index?
That could be done with the py_.filter method:
Sometimes your list of dictionaries can be nested like below. How can you get the taste attribute of apple ?
Luckily, this can be easily done with the py_.map_ method:
You can execute a function n times using the py_.times method. This method is a good alternative to a for loop.
Sometimes you might want to apply several methods to an object. Instead of writing several lines of code, can you apply all methods at once?
That is when method chaining comes in handy. To apply method chaining in an object, use the py_.chain method:
Note that running the code above will not give us the value:
<pydash.chaining.Chain at 0x7f8d256b0dc0>
Only when we add .value() to the end of the chain, the final value is computed:
This is called lazy evaluation. Lazy evaluation holds the evaluation of an expression until its value is needed, which avoids repeated evaluation.
If you want to use your own methods instead of pydash’s methods, use the map method:
To replace the initial value of a chain with another value, use the plant method:
Cool! We replace ['apple', 'orange', 'grapes'] with ['apple', 'orange'] while using the same chain!
Congratulations! You have just learned how to manipulate Python objects efficiently with pydash. I hope this article will add some more useful tools to your Python toolkit.
There are so many more cool methods pydash provides that I could not cover in this article. I encourage you to check out pydash’s API reference.
Feel free to fork and play with the code for this article in this Github repo:
github.com
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: