Python techniques: Introduction to Python list comprehension

Search for a command to run...

Automating the boring stuff in job applications, while keeping the final decision in your hands.

Automating the boring stuffs ...

Managing S3 Case Sensitivity in Python Workflows

🌩️ Cloud computing has revolutionized how applications are developed and deployed, providing a more flexible and scalable environment for running workloads. AWS offers several services for event-driven computing, including Amazon CloudWatch Events a...

Introduction📝 Looking to streamline your complex workflows and keep everything on track? Look no further than state machines - a powerful tool for precision and ease in process management. 💪🏼 And when it comes to implementing state machines, AWS S...

Loops essentially allow you to repeat a process over and over without having you write the same expression or condition, each time you want the program to perform a specific task.
Photo by Tine Ivanič on Unsplash
Suppose, we want to separate the letters of the string "programming" and add the letters as items of a new list. The first thing that comes to mind, might be writing a "for loop" 😀 for this operation.
Example 1: Iterating through a string Using for Loop
p_letters = []
word = 'programming'
for letter in word:
p_letters.append(letter)
print(p_letters)
When we run the program, the output will be:
> ['p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g']
However, Python has an easier way to solve this issue using "List Comprehension".
List comprehension is an elegant and concise way to define and create lists, based on existing lists, tuples, or strings 😎. It's powerful because it can auto-detect the type of iterable passed to it, which can be string, tuple, or list.
Syntax of List Comprehension
[expression for an item in list]
Let’s see how the above program can be written using list comprehensions.
Example 2: Iterating through a string using list comprehension technique
p_letters = [ letter for letter in 'programming' ]
print( p_letters)
When we run the program, the output will be the same as example 1.
In the above example, a new list is assigned to variable p_letters, as indicated by the square brackets. this new list contains the items of the iterable and in this case, it's a string 'programming'. Then We call a print() function to show the output.
Implementing this operation in python code using a rather traditional technique, while working with alterable, can grow to be quite verbose and less readable 😬.
Let’s see how the above program can be written using list comprehension.
Example 2: Get all sub-strings of the given string, using list comprehension + string slicing
word = "programming"
subs_list=[ word[i: j]
for i in range(len(word))
for j in range(i + 1, len(word)+1) ]
print(subs_list)
In the above example, a new list is assigned to variable subs_list, as indicated by the square brackets. the expression here is a string slicing method fitted with changing starting and ending indexes, to get all possibilities of the sub-strings from the word "programming". Then We call the print() function to show the output.
True, you can certainly do all of these operations using traditional loops 🤷♂️. However, not every loop can be rewritten as a list comprehension. But list comprehensions,
I hope you write clean and maintainable python code with this technique at your disposal 😁.
Thanks for the audience 🤗 and connect with me on Github, linkedIn and Twitter.