List Comprehensions and Built-in Functions on Lists
In this lesson, you’ll learn how to use list comprehensions to construct various lists, and different list methods to manipulate lists.
List comprehensions are a useful way to construct lists on the fly. They use the following syntax:
[<expr> for <elem> in <lst> if <cond>]
Here’s an example:
>>> [(lambda x: x *x )(x) for x in [1, 2, -5, 4]] [1, 4, 25, 16]
They’re used in place of functions like
filter(). To learn more, check out Using List Comprehensions Effectively.
There are also some useful built-in functions that can be applied to lists such as
max()finds the maximum value of an iterable using the built-in
cmp()method, or the
min()finds the minimum value of an iterable using the built-in
cmp()method, or the
any()returns whether any of the elements in the iterable are a
all()returns whether all of the elements in the iterable are
To learn more, check out Python Docs Built-in Functions.
01:11 So, here are three ways to do the exact same thing, but really, the last way—using list comprehensions—is the most Pythonic way and can show your interviewer that you know syntax that is used very frequently in Python.
Let’s look at another example. Let’s define a
is_odd() method, which will just return if
x % 2 is
1, basically because modulo takes the remainder, and if the remainder of dividing a number by
1, then it is odd.
Let’s go through a more complicated example that you would see in many interviews. Many questions include some sort of grid or some sort of matrix, so maybe, let’s have a two by three matrix just filled with
0’s, and define it
Let’s say if you were just given a variable
num_rows = 2, and
num_columns = 3. How would you build that? Well, let’s make our
grid an empty list, and use a
for loop, and then use a list comprehension. So you’d say
in range(num_rows) and then you have your current row, which is a list, and you say
for _ in range(num_columns).
num_columns times, and then we did that
num_rows times, and then all the
0’s were in one long list. What we really want is lists of lists, so we just forgot a bracket, there, and then we added an extra one at the very end.
Let’s move on to some useful built-in functions.
max() takes in some numbers and finds the max number, but it also can take in a list. So, looking back at that
lst we created at the beginning,
max(list) would be
4. What if we want to find the number that has the max square? Well, we pass in a
key param to
key=square—that would work, but I’m actually just going to redefine it as a lambda, just to show that you can do this all in one line and make it really clean, maybe in an interview or something like that.
any() is a useful function that takes in a iterable and returns
True if any of the values in the iterable are a
True value, and it returns
False if none of the values in the iterable are
True values. So, what does that mean?
any() of our list is going to look through our list
[1, 2, -5, 4], call
bool() on them, which is this method
bool(), which converts that to
False depending on if it’s a
True value or a
False value, and then
any() will return
True—yeah, based on if any of them are
False if none of them are
This might make more sense if we make a new list, like
[True, False] or
[False, False]. Let’s use
any() to check if any of our values in our list are odd. Well, how would we do that?
any() of our list, and then
like this, just do it all nice and in one line. Oh!
any(), for whatever reason, doesn’t take in a
key argument, not sure why.
max() do, but this does not work. So, we have to sort of do a weird shortcut.
We have to use list comprehensions to basically apply this function to each one of our values in our list, and then check if any of those values are
True. So here—and I’m going to do something sort of interesting here—I’m going to do it all in one line in case, you know, you have to do a one-line, or you have to complicate things, or you didn’t have time to define it beforehand.
And then, we would call
any() on that resulting list, and that would basically return, “Are any of our numbers odd?” Okay, you might be wondering, is there an
all() one that checks if all of them are odd?
Become a Member to join the conversation.