5 Advanced Features of Python and How to Use Them

We can apply our function to a single list or multiple lists.

In face, you can use a map with any python function you can think of, as long as it’s compatible with the sequence elements you are operating on.

(3) FilteringThe Filter built-in function is quite similar to the Map function in that it applies a function to a sequence (list, tuple, dictionary).

The key difference is that filter() will only return the elements which the applied function returned as True.

Check out the example below for an illustration:Not only did we evaluate True or False for each list element, the filter() function also made sure to only return the elements which matched as True.

Very convenient for handling to two steps of checking an expression and building a return list.

(4) ItertoolsThe Python Itertools module is a collection of tools for handling iterators.

An iterator is a data type that can be used in a for loop including lists, tuples, and dictionaries.

Using the functions in the Itertools module will allow you to perform many iterator operations that would normally require multi-line functions and complicated list comprehension.

Check out the examples below for an awesome illustration of the magic of Itertools!(5) GeneratorsGenerator functions allow you to declare a function that behaves like an iterator, i.


it can be used in a for loop.

This greatly simplifies your code and is much more memory efficient than a simple for loop.

Consider an example where we want to add up all of the numbers from 1 to 1000.

The first part of the code below illustrates how you would do this using a for loop.

Now that’s all fine and dandy if the list is small, say a length of 1000.

The problem arises when you want to do this with a huge list, say 1 billion float numbers.

With a for loop, that massive memory chewing list is created in memory — not everyone has unlimited RAM to store such a thing!.The range() function in Python does the same thing, it builds the list in memorySection (2) of the code illustrates the summing of the list of numbers using a Python generator.

A generator will create elements and store them in memory only as it needs them i.

e one at a time.

That means, if you have to create 1 billion floating point numbers, you’ll only be storing them in memory one at a time!.The xrange() function in Python uses generators to build lists.

Moral of the story: If you have a large range that you’d like to generate a list for, use a generator or the xrange function.

This is especially true if you have a really memory sensitive system such as mobile or at-the-edge computing.

That being said, if you’d like to iterate over the list multiple times and it’s small enough to fit into memory, it will be better to use for loops and the range function.

This is because generators and xrange will be freshly generating the list values every time you access them, whereas range is a static list and the integers already exist in memory for quick access.

Like to learn?Follow me on twitter where I post all about the latest and greatest AI, Technology, and Science!.Connect with me on LinkedIn too!.. More details

Leave a Reply