Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Acting on Iterables
00:00
In the previous lesson, I showed you some of the built-in iterator functions you typically use with for loops. In this lesson, I’m going to cover some of the iterator functions that return an iterable containing processed contents of the original.
00:14 Oftentimes, when presented with some data, you want to transform it. Rather than edit it in place, you can use a variety of functions that iterate over the data and return the processed contents.
00:24 This is done by having the function itself return an iterable object. I’m going to cover four functions. The first two are truthiness determiners, the third is for going backwards, and the fourth is for sorting.
00:39
The any() and all() functions are about truthiness, performing the equivalent of the bool() function on every item in an iterable.
00:47
Just as a refresher, calling bool() on a nonempty string is True, while calling it on an empty one is False. The all() function performs this operation on every item in an iterable, and-ing the results together, essentially determining if all of the items in the iterable are truthy.
01:09
1, 2, and 3 are all truthy, so calling all() on them is True.
01:17
This is the equivalent Boolean call, and-ing them together, one bool() at a time.
01:24
Since 0 is false, this returns False. If any item in the iterator passed to all() returns False, the end result is False.
01:37
And of course, it works for any truthy value. So all() means and together all the items, while any() means are any of the items true.
01:49
As 4 and 5 are True, it doesn’t matter that there’s a 0 in here, because any() is fulfilled by any truthy value.
02:01
A call to any() is the equivalent of or-ing together a call to bool() on each item.
02:09
And like with all(), any() works on more than integers. Anything that can be used for a truthy test will work. Before moving on, I’m going to take a quick aside. Python supports inline creation of iterables using something that looks a lot like a for loop.
02:26 This is known as a generator and is similar to list comprehensions. Let me show you an example.
02:35 Like with our iterator functions, what comes back from a generator is an iterable object. Let’s look at this one piece by piece. On the right-hand side, I have an iterable, in this case a list.
02:47
In the middle, I have the for loop syntax saying that this generator will iterate over the iterable, putting each value in x. And on the left, I have what is going to be the value returned for the iteration.
03:01
Here, I’ve put an inline condition, so I’ll get back True or False based on whether x is less than 10 or not. Note the surrounding parentheses.
03:10
This is important. Without them, you’ll get a SyntaxError if you try to use this on a bare REPL line. You don’t always need them, but to get at the generator object returned to the REPL like I’ve done here, you do need them.
03:25
Let’s use the list() function trick again.
03:31
And since the generator is being used as an argument, I don’t need the extra parentheses. The ones used for calling the list() function are enough to make Python’s parser happy.
03:41
The result contains three values. True because 1 is less than 10. True because 5 is less than 10. And then False because 11 is not less than 10. I took that little tangent because using a generator within a call to any() or all() is a common way of processing some values.
04:01
If you want to ensure that all values in your list are less than 10, you could combine all() with the same generator. Instead, if you only need some value in the list to be less than 10, you can use any() for that.
04:15
The reversed() function iterates over a collection and returns back each item in the reverse order.
04:22
And like you’d probably come to expect, it does this through an iterable object. Using the list() trick.
04:32
And you can see the items come back in reverse order. If, instead of reversing, you want to sort, you use sorted().
04:43 Now that might have been a little bit of a surprise. Yes, the return result is sorted as expected, but it’s a list, not an iterable. It’s not entirely clear to me why this is the case.
04:54
I thought maybe it predated iterables in Python, but nope, sorted() got added in 2.4 while iterables were added in 2.2. Something about consistency being a hobgoblin or some such thing. sorted() takes some arguments that allow you to modify how the sorting behaves.
05:11
The reverse argument allows you to sort in descending order. This is faster than calling reversed() on the resulting list and lets you skip a step.
05:21
By default, sorted() sorts by comparing each item to each other the same way you would with a less than operation. Consider this list.
05:39
When you compare strings, Python uses their code point number to determine their order, so capital letters come before lowercase. If you want to do case-insensitive sorting, you can pass a lambda to sorted() that modifies how the comparison works.
06:03
sorted() calls the lambda for each value before performing the comparison, thus allowing you to change its behavior. This can be particularly helpful if you need to sort objects by one of their fields.
06:15 Allow me to go on another quick tangent.
06:27
If you’re not familiar, data classes are shortcuts for creating classes that have defined field types. By wrapping the Person class with the dataclass decorator, I get a class that takes two arguments, the name and age.
06:40
Python does a bunch of things behind the scenes to make this possible. In fact, one of those behind the scenes things is defining a .__repr__() method.
07:01
Under the covers, sorted() is doing a less-than comparison, so this failed because I haven’t defined how to compare two Person objects.
07:09
Remember before I got tangential I was talking about the key argument? Well, it’s handy to use it as an accessor, telling sorted() how to get it the thing to sort by.
07:29
Here I’m using key to access the name attribute and using that to sort.
07:43
Or you could do it by age instead. For more examples on using all() and any(), see this tutorial. In addition to being able to use the built-in functions for reversing and sorting, lists also have in-place methods for doing the same.
07:59 To see the differences between these two ways of reversing, read this tutorial. Or the two ways of doing sorting, see this video course or tutorial. Next up, I’ll show you how to combine iterables and how to extract things from them.
Become a Member to join the conversation.