Managing Many or Conditions
In this lesson, you’re going to learn how to manage many
or conditions, or that is, how to avoid using the
or keyword and instead use
any(). To do this, you’re going to learn about how to put Boolean values into iterables or variables into iterables.
00:18 You’re going to learn about the iterable unpacking operator and how this can be very useful in these situations. And you’re also going to think a bit about how to avoid the situation in the first place.
00:31 The task you’re faced with is that you’ve reached a spot in your code in which you have all these variables, and here the values are shown. So perhaps it is that you’re trying to visit somewhere, and you’re evaluating whether a particular location has any of these attributes—whether it’s nice, it’s near, whether it has family.
This place doesn’t have very much, but it does have family, so it might be worth a visit. You’ll also note that
good_weather is in a list, and this is where the iterable unpacking operator will come into play.
So first, a quick look at the iterable unpacking operator and preparing lists to be iterated over. Say, for example, you have these two lists. They have strings in them. One list is
bowl, and that has two apples and a banana. The other list is
01:37 You might be tempted just to insert these items in a new list. However, what that creates is a two-dimensional list. As you can see, you have an outer list that contains two items, and each of those items is itself a list, one of which contains three items, the other contains two items. You can iterate over the outer list, but that will only return the list. It won’t return the actual values.
And what you’re interested in for the following exercise are the individual values. One way to get around this is to initialize the list then use the
.extend() method on that list and pass it in every other list.
02:32 then we have a one-dimensional list with all the values that is ready to be iterated over. What the iterable unpacking operator allows you to do is to write this all in one line. It is just syntactic sugar, which means it just makes things easier to read or easier to type, so there’s not as much code to go over.
So, start by initializing the
bag variable again. You initialize a list, and then within that, you use the two lists. However, instead of just executing it like this, in front of each list, you put the iterable unpacking operator (
*). And now when you look at
bag, it will be complete and flat, which means just one-dimensional with all the items, and it’s ready to be iterated over.
So back to the example at hand. If you remember, you have all these variables that have Boolean values within them, and what you’re looking for is a way to analyze all these variables and return a
True value if any of these values are
True. As you can see,
True, so you can expect the
As a shortcut, you don’t have to go through the stage of creating a new variable, and you can instead just call the
any() function in-line, initialize a new list, and put all the variables in there with the unpacking operator (
good_weather, and that will also return
Note there’s an interesting effect here, that say you don’t use the iterable unpacking operator (
*), and you take out
has_family because that’s the only one that’s
True. So what you’re left with
is a list of mostly false values, but it also contains a list that contains
False values. You might be tempted to think that this would evaluate to
False if you do it with
any(), but it will actually evaluate to
And this is because of the way that
any() evaluates values that aren’t Boolean values directly. So it’s going through every single item in the
False—and then it gets to a list, and it says, “Okay, how am I going to translate this list into a Boolean value?” It doesn’t look within that list. It just says, “Does this list have any items?
If it has items, I’m going to say it’s
True.” If it’s an empty list, it will say it’s
False. Anyway, there’s going to be more about how
or for that matter, deal with Boolean evaluation in a later lesson.
And that’s managing many
or conditions. In this lesson, you put variables into a list. Now you did this before calling the
any() function by creating a new variable and creating a list within that variable.
But you also did this in-line, skipping the variable creation step and calling
any() and initializing a new list in-line. You also used the iterable unpacking operator to expand a smaller list into a larger list.
06:39 You also thought about how to avoid the situation in the first place. It’s not ideal to end up with lots of variables. You can perhaps populate a list as you create the checks, or if you still want the names of the values, then you can create a dictionary.
Become a Member to join the conversation.