You may have heard the quip "I will always choose a lazy person to do a difficult job because a lazy person will find an easy way to do it." It's frequently attributed to Bill Gates, though there's a pretty good chance it originated as far back as the 1920s. Regardless of the brilliant mind who first came up with the humorous notion, you've got to admit that there's something to it.
I'll be honest--I don't typically think to myself "how can I do this lazier?" I do, however, aspire not to do things the hard way and to learn from the mistakes I make as I tumble through project after project at work and at home. I find myself looking at lots of lists of similar things all the time in my day to day work, and needing to understand the similarities and differences between them. Today I thought I'd share one of the basic capabilities that makes Python so good at doing this kind of compare and contrast work: list comprehensions! You might be thinking, "Ok, but I already comprehend what a list is." Well, you're
hilarious mildly amusing, but what I'm getting at is that Python has a built-in way to understand the way you'd like to build out a list, and that can help us get lazy in a good way. Let's run through an exercise:
First, let's make some lists that we want to compare. These can be anything, but for today, I'll start by creating a list of every even number up to 50 and a list of every odd number up to 50. How would you go about doing this? At first, especially when doing these things ad hoc, you might find it convenient to simply type the required numbers into a list. You know, 2, then 4, then 6, etc. Booooring. Also, totally unscalable. If the exercise called for all even numbers or odd numbers up to 50,000, you'd laugh if I told you to type them in manually. Rightly so. Obviously, there must be a better way to do it. Your next instinct might be to programmatically build the lists out by simply tacking things onto the end of a list you've arbitrarily started. This is also known as appending.
While it works, it's nowhere near as elegant as using list comprehensions. They make things so much better. If I were to try and talk through the way I did the task above, it would sound like this:
"Ok, Python, I want you to iterate 25 times, and, uh, and each time, go ahead and multiply the value of the number of the specific iteration by 2 (and possibly add 1 to that product, depending on which flavor of list I'm looking for), then append that outcome to that blank list I set up before sending you off on this mission."
That's...certainly a way to do it. Doing it an easier way might sound something like this:
"Hey, Python, make a list of any number up to 50 that is (or isn't) divisible by 2 with no remainder."
In code, it looks like this. Notice that the square brackets actually surround the stuff you want to throw into the list.
Wow, that is...less code for starters, and, in my opinion, it's much easier to understand what's happening. Now let's find a third list of interesting numbers somewhere. How about a unique list of numbers from the Fibonacci Sequence whose values are less than or equal to 50? (I'm specifying that unique bit because the sequence has two ones right at the beginning, and I don't need repeats.)
Those playing along at home might recognize the "just find the next one and append it to the end of a list" strategy I made fun of earlier. I'm not perfect. I'm lazy. If you've got a better solution, leave it in the comments!
Now that we've got 3 lists of things to compare, let's generate some new derivative lists. List comprehensions make this very simple to accomplish. We'll create a list which includes any value found in both the Fibonacci list and the Evens list, and a second list which includes any value found in both the Fibonacci list and the Odds list.
While it's true that you can also accomplish similar tasks many different ways in Python (another favorite of mine is set operations), this kind of quick syntax makes my life easier. Let me leave you with one example of how I've used list comprehensions in recent memory. You may be aware that PokitDok offers an Eligibility API. I was tasked with analyzing the contents of real-time medical insurance eligibility responses and how they change when the request specifies a different service type. Using list comprehensions, I found some surprising differences and similarities and was able to show that what you ask for truly matters, in that it affects what data will be returned to you.
Do yourself a favor. Code smarter, not harder. Let the lazy ease of list comprehensions do the hard work.