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

Unlock This Lesson

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

Unlock This Lesson

Filtering II: For Loop Example

Give Feedback

This lesson continued the discussion on filtering, this time using a for loop to generate a list of squares. The for loop does exactly what the list comprehension from the last lesson did and the output is the same.

You saw that, using a for loop, the code turns out this way:

>>>
>>> even_squares = []
>>> for x in range(10):
...     if x % 2 == 0:
...         even_squares.append(x * x)

Comments & Discussion

Become a Member to join the conversation.