Python’s Coolest Features (Intermediates)

Mehul Gala
4 min readNov 10, 2019

--

In the last article, we saw Python’s coolest features built around variables, operators, loops, and functions. In this article, we’ll cover intermediate features that are built around data structures and iterations which eases out working with them, and to solve a problem in shorter lines of code.

Universal `in’ operator

One of the most common tasks dealing with any data structure is to check for the existence of a particular item in it. Python gives us an “in” operator that can be used for this task with all of its built-in data structures (list, tuples, sets, and dictionaries), which can be handy as we don’t have to remember the different syntax for all of them.

List Comprehension

Lists are probably the most widely used data structures in Python. You can create lists on the fly using the List comprehension feature which is pretty dynamic.

Think of them whenever you want to operate on a list of items, with occasional filtering, and generating a new list.

Enumerate function

Traditionally, developers have relied on the simple for loop that plays with the index to iterate through a list of items. But this often results in the nagging “Off by One” error. To tackle that, many languages now have included “For Each” loop which handles ‘indexing’ on its own and just returns the ‘item’ in every iteration. Python’s ‘for in’ loop works the same way.

But what if you want both, the ‘index’ as well as the ‘item’ while iterating over. Make use of the enumerate function which returns both.

Zip function

Zip, as the name suggests, allows you to zip or combine one of more iterables, and do parallel processing on them. It is another versatile function of Python which is very popular among developers.

We can also use Zip, to sort two lists in parallel. This is also a common task which you’ll need every now and then.

We’ll talk about the act of unzipping when we cover the packing and unpacking of iterables in few sections below.

Keyword arguments

Most of the programming languages follow the strict ordering of the parameters to a function. This becomes a bit of an issue as the developer has to remember the order or go back to see the declaration of the function.

Python introduces keyword arguments in which we can pass arguments as key-value pairs. The idea is to allow the caller of a function to specify the argument as key-values so he does not need to remember the order of parameters.

Packing and Unpacking

This feature singlehandedly increases the flexibility of data passing between different functions extremely easier to handle.

Packing is a feature to convert individuals variables to a list or tuple on the fly. It is useful to pass the variable number of arguments to a function.

Unpacking is used to convert a list or a tuple back to individual variables.

It works at both the places white sending data to a function or receiving data returned from a function.

Dictionary Comprehension

Like lists, even dictionaries can also be created on the fly. Dictionaries are very useful data structures, and very often we need to convert them back and forth with lists and tuples. This technique comes in handy to achieve that with a single line of code

Operator Chaining

We can chain multiple operators to check boundary conditions in shorter lines of code. The need to add logical operators to combine two conditions are avoided.

This concludes the intermediate feature list. Python is filled with many other advanced features which are used for specific use cases, and not to forget its vast standard library that can be tapped into to achieve the tasks we want to accomplish in a much shorter code length. More on that in future articles.

The Zen of Python

If interested, read the Zen of Python, to know about its design philosophies. The same came also be read by typing the following command in a python script.

--

--