Resource linked in this lesson: Iterators and Iterables in Python: Run Efficient Iterations
Traversing a List
00:00
Traversal or iteration means accessing each element in a sequence one by one. In this lesson, you’ll look at three styles of list traversal in Python: for
loops, list comprehensions, and the functional tools map()
and filter()
.
00:14 Because iteration and iterators is such a deep subject, this lesson will be more of an overview. If you’re interested in a deeper dive, take a look at the tutorial: Iterators and Iterables in Python: Run Efficient Iterations.
00:28
Alright, off to the REPL we go. We’ll start by again creating a list of numbers. Call it integers
. integers
equals the list [4, 8, 15, 16, 23, 42]
.
00:42
You can iterate over integers
like so. for number in integers:
00:50
print(number)
. In this example, number
is a temporary variable that takes on the values from integers
one at a time.
00:58
Then the code in the indented block runs, printing that number. This is why the output shows each number printed on its own line. Another common pattern with this is to iterate over the result of passing a list to the enumerate()
function, which returns an index as well as the element: for index, number in enumerate(
integers): print(
f"The value of index {index} is the index of value
of {number}"
), and so what’s printed is zero is the index of 4, 1 is the index of 8, etc.
01:33
What if you wanted to use a for
loop to take this list and return a new list with only even numbers? You might do something like this. First, create an empty list: even_numbers = []
Iterate over integers
: for number in integers:
01:49
if number % 2 == 0:
even_numbers.append(number)
.
01:59
The result is even_numbers
holds only the even numbers [4, 8, 16, 42]
. But you can actually pack all of this logic into a list comprehension like so: [number for number in integers if number %
2 == 0]
.
02:17
This too returns the list [4, 8, 16, and 42]
. And this could be called the Pythonic approach, but Python also provides tools from functional programming: map()
and filter()
among others.
02:30
For the next example, you need a list with mixed positive and negative integers. I’ll just clear the screen first. numbers =
[-10, 4, -7, 5, 9]
.
02:45
You could use a list comprehension with the built-in abs()
function to convert all of the negative numbers to positive, like so:
02:53
[abs(number) for number
in numbers]
, which returns the list [10, 4, 7, 5, 9]
all positive. But you could also use map()
.
03:04
map()
takes a function and a sequence as arguments and it applies that function to the elements of the sequence. It returns an iterator, so you’ll have to pass the output of map()
to the list()
constructor to get a list.
03:16
Pass the abs()
function and numbers
to map()
and the result of that to the list()
constructor. Store the output in the variable absolutes
.
03:26
Look at absolutes
and you can see all the values are positive. And if instead you wanted to filter out the negative values, you could use the filter()
function instead.
03:36
filter()
works like map()
except it’s used to filter a sequence. This is done by passing each element to a function and keeping only the elements where that function returns True
.
03:45
Because filter()
also returns an iterator, you’ll need to pass its output to the list()
constructor.
03:51
positives = list(filter(lambda number:
number >=
0,
and the variable numbers
. This lambda function returns True
if the value passed to it is greater than or equal to zero.
04:07
And filter()
uses the outputs of the lambda function to decide whether to keep the elements of numbers
. So the result is the list [4, 5, 9]
.
04:17 Alright, you’re getting close to the end of the course. There’s just a few more features of lists to discuss. I’ll see you in the next lesson.
Become a Member to join the conversation.