The Way of the Serpent

Pythonic Tips & Tricks — Using Comprehension

Interesting ways to use List Comprehension

Tonichi Edeza
Towards Data Science
4 min readFeb 9, 2021

--

Photo by Cathryn Lavery on Unsplash

List comprehension is a powerful tool that allows Python programmers to write codes in an extremely condensed fashion. In this article we will go over how to use list comprehension to simplify otherwise complex functions.

Let’s begin!

To start off, let’s create a sample string.

sample_string = 'sample_for_list_comprehension'

Now let us create a Python list that contains every character of that string as its elements.

sample_list =[]
for s in sample_string:
sample_list.append(s)
sample_list
Creating a List

We can actually generalize this code and turn it into a Python function. The below code does just that.

def list_creator(input_string):
final_list = []
for i in input_string:
final_list.append(i)
return final_list
sample_list = list_creator(sample_string)
sample_list
Python Function

Though the function fulfills its task, we can actually write it in a much more succinct way via the use of List Comprehension. The below code does just that.

def list_comp_creator(input_string):
return [i for i in input_string]
sample_list = list_comp_creator(sample_string)
sample_list
Function with List Comprehension

We see that the function is now essentially a one-liner. Of course the example is rather simple. Let us now use list comprehension to do more complex tasks.

Integrating If-Else Logic

We can actually structure our list comprehensions to include If-Else logic. For example, let us create a function that will only return characters that have an even index (note that the Python programming language starts at 0).

def list_comp_creator_even(input_string):
return [i for n, i in enumerate(input_string) if (n%2 == 0)]
even_list = list_comp_creator_even(sample_string)
Integrating Logic

Note how we also used the enumerate function. For those of you who are not familiar with the function, what it essentially does is act as a counter. If the function it is a part of has a For loop, the enumerate function will return what current iteration it is currently in. This can be demonstrated by the below code.

for n, i in enumerate(range(-10,5)):
print(n , i)
Enumerated Output

The numbers on the left represent the index, essentially they show at what stage of the For Loop our function is currently in. Note how enumeration for Python begins at 0. This allows us to craft code such as the previous list comprehension function. To take it one step further let us explicitly code what the function should do if it comes across an odd-indexed character.

def list_comp_creator_even_v2(input_string):
return [i if (n%2 == 0) else 'odd'
for n, i in enumerate(input_string)]
even_list_v2 = list_comp_creator_even_v2(sample_string)
Integrating If-Else Logic

We can see that the function will now return all the even-indexed characters and replace all the odd-indexed characters with the string ‘odd’.

Creating a List of Tuples

List comprehension can do more than create a plain vanilla list. We can actually use list comprehension to generate more complex output as well. The below code will take the input string and return a list of tuples that contain (character index, character).

def index_creator(input_string):
return [(f'{n}', i) for n, i in enumerate(input_string)]
indexed_list = index_creator(sample_string)
indexed_list
Creating a List of Tuples

Excellent, we have used list comprehension to create a list of tuples. Though this is already useful, let’s try to increase the complexity. Let us create a list of tuples that contain (character index, character, uppercase character, index parity).

def index_creator_v2(input_string):
return [(f'{n}', i, i.upper(), 'Even' if (n%2==0) else 'Odd')
for n, i in enumerate(input_string)]
indexed_list = index_creator_v2(sample_string)
indexed_list
Complex List Comprehension

Note that we are not limited to simply creating a list of tuples, we can also create a list of lists.

def index_creator_v3(input_string):
return [[f'{n}', i, i.upper(), 'Even' if (n%2==0) else 'Odd']
for n, i in enumerate(input_string)]
indexed_list = index_creator_v3(sample_string)
indexed_list
List of Lists Output

Excellent, we are definitely getting into the more complex side of list comprehension.

Loading the List into a Pandas DataFrame

Of course to facilitate data analysis, let us load the output into a pandas DataFrame.

import pandas as pddef panda_dataframe_creator(input_string):
final_list = [[f'{n}', i, i.upper(), 'Even' if (n%2==0)
else 'Odd'] for n, i in enumerate(input_string)]
return pd.DataFrame(final_list,
columns =['character_pos','character',
'upper_character','index_parity'])
df = panda_dataframe_creator(sample_string)
df.head(5)
List of Lists loaded as Pandas DataFrame

In Conclusion

List Comprehension is an extremely powerful tool for any data scientist working with the Python language. This article only scratches the surface of its possibilities. In future articles we shall tackle much more challenging and complex problems that will require us to make use of list comprehension.

--

--