Grouping adjacent list items using zip, 

no more shit, just read the code. 
>>> a = [1, 2, 3, 4, 5, 6]
>>> zip(*([iter(a)] * 2))
[(1, 2), (3, 4), (5, 6)]

>>> group_adjacent = lambda a, k: zip(*([iter(a)] * k))
>>> group_adjacent(a, 3)
[(1, 2, 3), (4, 5, 6)]
>>> group_adjacent(a, 2)
[(1, 2), (3, 4), (5, 6)]
>>> group_adjacent(a, 1)
[(1,), (2,), (3,), (4,), (5,), (6,)]

>>> zip(a[::2], a[1::2])
[(1, 2), (3, 4), (5, 6)]

>>> zip(a[::3], a[1::3], a[2::3])
[(1, 2, 3), (4, 5, 6)]

>>> group_adjacent = lambda a, k: zip(*(a[i::k] for i in range(k)))
>>> group_adjacent(a, 3)
[(1, 2, 3), (4, 5, 6)]
>>> group_adjacent(a, 2)
[(1, 2), (3, 4), (5, 6)]
>>> group_adjacent(a, 1)
[(1,), (2,), (3,), (4,), (5,), (6,)]

Even after using Python for years, we stumble upon functions and features that we did not know about. Some of these can be quite useful, yet underused. With that in mind, I’ve compiled a list of incredibly useful Python functions and features that you should be familiar with.

Functions with Arbitrary Number of Arguments

You may already know that Python allows you to define functions with optional arguments. But there is also a method for allowing completely arbitrary number of function arguments.
First, here is an example with just optional arguments:
Now, let’s see how we can build a function that accepts any number of arguments. This time we are going to utilize Tuples:

Using Glob() to Find Files

Many Python functions have long and descriptive names. However it may be hard to tell what a function named glob() does unless you are already familiar with that term from elsewhere.
Think of it like a more capable version of the listdir() function. It can let you search for files by using patterns.
You can fetch multiple file types like this:
If you want to get the full path to each file, you can just call the realpath() function on the returned values:

Debugging

Some of the examples below make use of the inspect module. This module can be very useful for debugging purpose and you can get with it much more that what described here.
We are not going to cover each one of these in this article, but I will show you a few use cases.

Generating Unique ID’s

There may be situations where you need to generate a unique string. I have seen many people use the md5() function for this, even though it’s not exactly meant for this purpose
There is actually a Python function named uuid() that is meant to be used for this.
You may notice that even though the strings are unique, they seem similar after several characters. This is because the generated string is related to the computer network address.
To reduce the chances of getting a duplicate, you can use this two functions.

Serialization

Have you ever needed to store a complex variable in a database or a text file? You do not have to come up with a fancy solution to convert your arrays or objects into formatted strings, as Python already has functions for this purpose.
This was the native Python serialization method. However, since JSON has become so popular in recent years, they decided to add support for it. Now you can decode and encode as well inJSON:
It is more compact, and best of all, compatible with javascript and many other languages. However, for complex objects, some information may be lost.

Compressing Strings

When talking about compression, we usually think about files, such as ZIP archives. It is possible to compress long strings in Python, without involving any archive files.

Register Shutdown Function

There is a module called atexit, which will let you execute some code right before the script finishes running.
Imagine that you want to capture some benchmark statistics at the end of your script execution, such as how long it took to run:
At first this may seem trivial. You just add the code to the very bottom of the script and it runs before it finishes. if there is a fatal error, or if the script is terminated by the user, again it may not run.
When you use atexit.register(), your code will execute no matter why the script has stopped running.

Conclusion

Are you aware of any other Python features that are not widely known but can be quite useful? Please share with us in the comments. And thank you for reading!
Repost from http://pypix.com/tools-and-tips/python-functions/

Popular Posts