• + 6 comments

    Not wrong, but you have not used list comprehension. Consider a list called 'a' with even and odd numbers. Now suppose you want to create a list 'b' that contains only the even numbers of list 'a'.

    One way to do this in the usual way with a loop:

    • b = []
    • for n in a:
    • if n % 2 == 0:
    • b.append(n)

    Using list comprehension this would look like this single line:

    • b = [n for n in a if n % 2 == 0]

    The challenge in this problem is creating nested list comprehesions, which can be tricky. See PEP 202.

    (Sorry for the broken formatting, but it's not possible to code-format in replies here in HR.)