Write cleaner code using Python Pipes

Filed Under: Python Modules
Pipes Fealmg

In this article, we’ll understand how to use Python pipes to create cleaner code. After using numerous methods to process a complex Python iterable, do you have jumbled code? If so, give Pipes a shot!

Map and filter are two useful Python methods for dealing with iterables. However, if you use both map and filter simultaneously, the code can become jumbled.

Nobody likes looking at sloppy or unorganized code. The Pipe library is ideal for cleaning up any messy code that comes your way.

Understanding Pipes

Pipe is a Python module that allows you to use pipes in your Python code. A pipe (|) connects the output of one method to the output of another.

When applying Pipe to numerous methods of a Python iterable, it might make your code look clearer. Furthermore, because Pipe provides only a few methods, it is incredibly simple to learn and understand.

To begin, we will use the pip command to install the Pipe Library.

pip install pipe

Applying Pipes to Various Functions

As I said earlier, the Python Pipe library comes with only a few methods of the Python programming language. Let’s have a look at them one after another.

Method 1 – Where Function in Pipes

If you are familiar with SQL statements and clauses, you might have seen the WHERE clause there. The WHERE clause helps a programmer filter records. In Pipes as well, the clause works exactly the same way. Have a look at the code given below:

from pipe import where
l = [1,2,3,4,5,6,7,8,9,10]
filter_l = list(l | where(lambda x: x%3 == 0))
print(filter_l)

On executing the code mentioned above, the output will come out as [3, 6, 9]. You can see only the number divisible by 3 were filtered out.

Method 2 – Select Function in Pipes

The map method is comparable to the select method. The selection applies a method to each iterable element. Look at the code written below:

from pipe import select
l = [1,2,3,4,5,6,7,8,9,10]
select_l = list(l | select(lambda x:x*5))
print(select_l)

On executing the code mentioned above, the output will come out as [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]. You can see that all the numbers are multiplied by 5 with the help of the select command.

Combining Select and Where Functions

You may be curious why we need methods where and select if they provide the same functionality as map and filter.

This is because pipes allow you to place one function after another. As a result, implementing pipes eliminates nested parentheses and improves code readability.

from pipe import select
l = [1,2,3,4,5,6,7,8,9,10]
new_l = list( l | where(lambda x: x%3 == 0)
                | select(lambda x:x*5)
            )
print(new_l)

Method 3 – Groupby Function in Pipes

At times, using a specific function to group entries in a list can be handy. That would be simple to accomplish using the groupby approach.

To demonstrate how this strategy works, let’s convert a list of numbers into a dictionary that categorizes numbers based on whether or not they are divisible by 3.

from pipe import groupby,select
group_l = list(
    (1,3,5,7,9,11,12,15)
    | groupby(lambda x : "Div3" if x%3==0 else "NotDiv3")
    | select(lambda x: {x[0] : list(x[1])})
)

print(group_l)

In the preceding code, we use groupby to divide numbers into the Div3 and NotDiv3 groups. Following that, we use select to convert a list of tuples into a list of dictionaries, with keys being the first elements in the tuples and values being the second elements in the tuples.

The following is the result of using this method:

[{'Div3': [3, 9, 12, 15]}, {'NotDiv3': [1, 5, 7, 11]}]

Method 4 – Dedup Function in Pipes

The dedup method is used to eliminate duplicates from a list. That may not sound fascinating because the set approach can accomplish the same thing. On the other hand, this method is more adaptable because it allows you to obtain unique elements by utilizing a key.

You can, for example, use this procedure to obtain a unique element that is less than 3 and another unique element that is greater than or equal to 3.

from pipe import dedup

l = [1,2,3,4,5,1,2,4,3,1,5,6,1,3,2]
dedup_l1 = list(l | dedup)

dedup_l2 = list(l | dedup(lambda x: x<3))

print("After removing duplicates : ", dedup_l1)
print("Smaller than or equal to 3 : ",dedup_l2)

The output comes out to be as follows:

After removing duplicates :  [1, 2, 3, 4, 5, 6]
Greater than or equal to 3 :  [1, 3]

Conclusion

Congratulations! You’ve just learned how to use pipes to keep your code clear and concise. I hope this post has given you the expertise to transform complex iterable operations into a single line of code.

close
Generic selectors
Exact matches only
Search in title
Search in content