I am cycling along the River of The Prairies. Enjoying the wind blowing against my body. Slowing me down. I feel trapped. I feel like my coding skills are plateauing. Trapped in a career rut of my own making. Unlike the river running beside me. The river flows freely to wherever it wants to be. Never slowing down.
I know this cycling route along the river by heart now. It feels comforting. It also feels routine. Almost as routine as my daily work grind. But today I am looking for an escape from this routine. Instead of continuing down this familiar route, I decide to cycle onto a new route, a route that feels scary and not comforting, but holds the promise of discovery, a route that leads to an escape from the trap that I am in. On this route, I discover a group of Mexican folk dancers. They dance on a mini amphitheater with all the joys in life. Life is a lot like a bike ride, I can’t make new discoveries if I keep pedaling on the same route. Just like how I won’t learn how to use list comprehension in Python when I keep using for loop all the time to create a new list. Today I’m practicing using list comprehension with this Python exercise for beginners on lists: Remove Even Integers from List.
Life is a lot like a bike ride, I can’t make new discoveries if I keep pedaling on the same route. Just like how I won’t learn how to use list comprehension in Python when I keep using for loop all the time to create a new list. Today I’m practicing using list comprehension with this Python exercise for beginners on lists: Remove Even Integers from List.
The Problem Statement
Given a list of random integers, implement a function remove_even_integers(input_list) that removes all even integers from the given list.
Input | Output |
[1,2,3,4,5,60] | [1,3,5] |
[2,4,6,8,10] | [] |
[1,3,1,7] | [1,3,1,7] |
List comprehension in Python is a way to create a list from another list. Which is exactly the use case that I want to use to solve this python exercise. I want to create a new list with no even integers from a given list. The basic syntax for list comprehension looks like this.
myList = [ expression(x) for x in myOldList if condition(x) ]
The Solution: Using For Loop
I’ll start with a for loop solution to this problem first. This is simple to implement. I want to iterate through the elements in the list, checking for any even integers as I iterate through the list. I can use the modulo operator to check for even integers. Just check that the integer modulo of two equals zero. This works because any even integer divided by two should return a remainder of zero. If I get a remainder of zero, simply remove the integer from the list. Here’s the code in Python:
def remove_even_integers(input_list):
#Iterate through all integer elements in input_list
for element in input_list[:]:
if element % 2 == 0:
input_list.remove(element )
return input_list
The Solution: Using List Comprehension
The logic to implement the solution using list comprehension is the same as with the for-loop solution. Except this time, I can write the code in a more concise manner. Here’s the same solution but written using list comprehension:
def remove_even_integers(input_list):
# List comprehension to iterate through list
# and add to new list if it’s not an even integer
return [element for element in input_list if element % 2 != 0]
That wasn’t too scary to implement. In fact, l dare say I am almost comfortable with using list comprehension at this point. I know this because I took another stab at using list comprehension for another problem in this article: Interview Questions for Python: Reverse List.
Testing The Solution
As a software engineer, I can’t let things end with just the implementation of the solution. I need to verify that my implementation works as I expect it to. A simple test will suffice here to silence the inner QA tester in my that’s nagging me to test every single code that I write. I can use the input and output from the table earlier as test cases to check that my code works. Nothing complicated here. I don’t want to invest time to come up with complex test cases here. It’s not like I’m trying to launch a rocket with this code. I’m just practicing how to use list comprehension. Here’s the simple test function that I came up with:
def test_remove_even_integers():
input_list1 = [1,2,3,4,5]
input_list2 = [2,4,6,8,10]
input_list3 = [1,3,1,7]
output_list1 = remove_even_integers(input_list1)
output_list2 = remove_even_integers(input_list2)
output_list3 = remove_even_integers(input_list3)
assert output_list1 == [1,3,5]
assert output_list2 == []
assert output_list3 == [1,3,1,7]
Takeaway
In my experience as a software engineer, there will always be new things to learn. Sometimes this can feel intimidating. It’s not always easy. I find the temptation to stick to the boundary of things that I already know to be detrimental to my growth as a software engineer. To combat this, I like to remind myself of the memories of joy, from new discoveries that I encounter as a result of building the courage to keep learning new things. I know that when the fear is real, it’s best to lean into it and embrace it.
This is my approach to learn new coding skills like list comprehension in Python. List comprehension is a very convenient technique to use when I need to create a new list from an existing list. I can’t even imagine going back to using for loops to do this now.