Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Managing Many or Conditions

00:00 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.

00:52 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.

01:07 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 shelf.

01:26 It has some chocolate and cookies. So these are all strings, and you want them to be in a list called bag. So you can imagine you’re packing your bag and you want all these things in your bag.

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.

02:04 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:22 Now bag contains all the items that bowl contained, and if we repeat that operation with the shelf,

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.

02:56 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.

03:27 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, has_family is True, so you can expect the True value.

03:46 Also know that good_weather is a list, however, so you’re going to need to take that into account while you’re building the iterable that you’re going to feed into the any() function.

03:57 Now it’s not ideal, but the way that you do this, being in a situation that you have all these variables, is by initializing a new list

04:08 and inserting all of the variables manually. You’ll note the iterable unpacking operator (*) before good_weather to make sure that this is just a one-dimensional list.

04:20 And now if you look at condition, you’ll see that you have a flat list of Boolean values that can easily be fed into the any() function and return True as expected.

04:38 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 (*) on good_weather, and that will also return True.

04:58 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

05:12 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 True.

05:28 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 conditions list—False, False, False, False, False, Falseand 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?

05:54 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 any(), and or for that matter, deal with Boolean evaluation in a later lesson.

06:11 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.

06:24 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.

06:56 Next up, you’re going to be looking at list comprehensions and their use with the any() function.

Become a Member to join the conversation.