Python Beginner Breakthrough (List Comprehensions) | by Miguel Saldana | Dec, 2020


A quick Python “A-ha!” moment that can make you a more efficient and “Pythonic” coder for your data science endeavors.

In the past twelve months, I have started transitioning my professional focus from a traditional engineering role into one that is looking to utilize data science and machine learning more and more. In doing so, one of the biggest skills that one needs to have some rooting and understanding of is coding. Coding is the essence behind the algorithms and models that one would develop in this field to help drive efficiencies and productivity.

Now to the task at hand, this article is intended to highlight something that I learned through my coding journey that has vastly helped me to improve:

  • My coding skills
  • Create more eloquent/Pythonic code
  • Trim out unnecessary lines of code

Probably one of the most basic and integral parts of coding is the utilization of lists. Whether they are imported and built from a data set, or the product of some process, lists are used all the time and have great utility. As a beginner, the rules around initializing and building lists are quite simple, but when you stumble across what list comprehensions do, it is mind-blowingly awesome. Through list comprehensions, one could simplify almost 5–6 full lines of code to a single, yet more complex, line. A cleaner, more declarative way to build lists (and also sets and dictionaries) that are easier to read and understand.

Basic Syntax of List Comprehensions:

The beautiful thing about list comprehensions is that the syntax compacts the way one initializes an empty list and subsequently populate it. The basic format is laid out in the code block below:

list = [expression for item in iterable if condition]

The basic parts of the comprehension are:

  1. Expression — The desired outcome that will be processed through the iteration
  2. Item — The object or value in the iterable
  3. Iterable — Any iterable object such as a list, set, sequence, etc
  4. condition — Ability to filter based on a logical argument

The code block below is an example of a function that creates a list of random numbers.

def random_list(num_variable, sample_size):
new_list = []
for idx in range(0, sample_size):
new_list.append(random.randrange(0, num_variable))
return new_list

In this method of building a list, albeit is a very traditional way a couple of key features can be noticed:

  1. the list, new_list is initialized in the first line of the function
  2. the values in the list are populated through the iteration of the for-loop and appended into the list

Note: Additional conditional statements (like an if-statement could be included to expand the lines of code a couple of lines

Utilizing a list comprehension the code within the function can be compressed into fewer lines as well as have more utility for future revision. Fewer lines to sift through for understanding as well as coming off more Pythonic.

The code would then look like the following:

def new_random_list(num_variable, sample_size):
new_list = [random.randrange(0, num_variable) for idx in range(0, sample_size)]
return new_list

Notice, that within the syntax of the list comprehension the expression of the for-loop is placed within the brackets when the list is initialized. An example of a more complicated list comprehension that also includes a conditional statement is below.

clean_in_lst = [neighbor for neighbor in in_neighbor if Node != neighbor]

The code block above is creating a “clean” list from an existing list called in_neighbors, the conditional term is filtering out all the items (called Node) that are not neighbors.

Leveling Up Your Comprehension

The exciting thing about once you understand how to use comprehensions is the versatility that they have in conjunction with more powerful objects and structures like dictionaries and sets. The same type of logic can be used to create a set or dictionary using the comprehension logic in fewer, easier to read code.

In addition, the really powerful part of the comprehension is to combine it with lambda functions and normal functions. As part of the expression section of your comprehension, you can insert a lambda or normal function to get your values into the desired state, especially if you are generating numerous lists with similar expressions and can simplify with a helper function in this instance.

Overall, list comprehensions (or just the concept of the comprehension in general) are a very powerful tool that can help you make more concise, easier to read code.

Read More …


Write a comment