Level-up Lists with List Comprehension!
Solving problems using Python is very much like a road trip. There are multiple routes one can take, but if you’re like me, you probably want the most efficient route. In this blog post, I will walk you through different ways I have utilized lists to make calculations in Python. We will use a road trip to help us frame our thinking — list comprehension is our destination!
You are on snack-duty for this trip and collect snack-requests for your travel-buddies. You list your friends’ favorite snacks and find out that friend0 wants additional packs of every snack on that list. Let’s declare a list for friend0 and use Python’s built-in methods, .append() to add all the snacks to the end of the list.
Task complete! friend0 has all the favorite snacks on their list!
If you’re unfamiliar with for loops, essentially you can interpret for loops as “for each item in the list, do what is indented.” In our for loop, each_snack refers to each item in our list called snacks.
I should note here that instead of using each_item in the for loop, it is customary to use the singular of the list’s name (my list was called snack_friend0, and it’s customary to use snack.)
Let continue on our way to make calculations! In the next example, you’ll notice a variable called i, which is a counter for each iteration, or step. While you can use any name you’d like for your counter, it’s customary to call it variable i. Here’s another example of a for loop using the variable i. Can you predict what the for loop will do? (Answer in lines 12–16!)
With our understanding of for loops, let’s add our requests, and each friend’s request to a central list. If you’re coding along, now is a good point to think about how you will tackle this challenge. Here’s how I completed it:
Now that we have our shopping list, let’s calculate how much cash we’ll need to bring to the store with us.
Let’s focus on one of the snacks our friends have in common: mangoes!
All that work — and that is just one snack! Imagine what our notebook would be like if calculated the subtotal for each unique snack. The 7 lines of code used to calculate the mangoes’ price can be accomplished in way fewer lines using a combination of built-in functions and List Comprehension.
Notice how we reduced the for loop, if/else statement, and calculations into just one line! Let’s dive deeper to get let’s dive-into this Python-beauty to get a clearer understanding of what is going on here using a simply version of the code above.
Here we have a for loop contained inside a [list] that is assigned to the variable snacks. Let’s break it down even further:
We’ve seen something similar when declaring lists above. We can use any variable name, just like before. Essentially we’re saying here that the list snacks will contain item(s): x. But what is x? The rest of our list comprehension reads:
for x in shopping_list ]:
We’re saying here that we’ll refer to everything on the shopping_list as ‘x’.
Now, let’s build on this thinking.
In this case we only want to know about mango(es) in the list. We indicated this using the conditional, if. IF ‘x’ matches ‘mango’, it will be added it to our list.
Let’s level up, just a notch and calculate our cost.
Here instead of adding the name of each item, we are giving 1 each time an item satisfies the if statement. I wrapped the one-liner in sum() to have the total.
Let’s keep building and refer to our previous example.
Are you able to explain line 45 in your own words?
That’s Python’s list-comprehension, sometimes referred to a Python’s one-liner.
If you have any questions, please reach out!
Originally published at https://medium.com on March 31, 2022.