Https://www.codecademy.com/paths/analyze-data-with-python/tracks/ida-3-introduction-to-numpy/modules/ida-3-2-numpy-syntax/projects/election-binomial

Hi, I have a general question: throughout the course, every once in a while, when you have to do an assignment at the end of a learning module, coding requirements occur of stuff that hasn’t been taught before, to the best of my knowledge. For instance, in this particular assignment, at point 2 you have to write a line of code with this syntax:

total = sum([1 for n in list if n == foo])

of which I’m pretty sure hasn’t occured any time before in my course, that I followed step by step. Is it just me or do people recognize this?
For clarity, first I completed my Python 3 course and my code foundations, and then I started the ‘Analyze data with Python’ Skill Path.

Thanks!

It’s a list comprehension, this bit in particular that is [1 for n in list if n == foo]. The equivalent with a normal for loop would look like the following-

# I'm calling it lst instead of list because I don't want to 
# shadow the built-in list class
lst = []  
for n in lst:
    if n == foo:
        lst.append(1)

Although the techinical implementation differs a little to a normal for loop it’s really just a piece of so-called syntactic sugar. That is it was designed just to make something that was already possible a bit nicer for human users. I thought it was included in the course (if only briefly) but I might be wrong.

So I hope the lesson doesn’t actually require that exact line, just something similar being passed like the example above. So it’s not something you strictly need to know but they’re common enough that you’d probably want to be able to read them at least.

Edit:
Found them though it’s a bit brief and buried in loops rather than lists-
https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-loops/exercises/list-comprehension-ii
I think it might be worth having a web search for a little more detail. Even if you don’t like using them, others might and you’d want to be able to understand the syntax.

That makes sense, tgrtim. It just struck me as odd that all of a sudden something like this appears seemingly out of nothing (well, out of relative ancient history ;-)), even though it’s not that difficult to comprehend. And because this happens every once in a while, I always start searching frantically in my syllabi, whether I missed out on something…

Anyway, just checking if this is actually just me or if people can relate. Have a nice weekend!

1 Like

That’s fair, I think it does happen on a few occasions where things are glossed over or briefly introduced but I suppose teaching every possible part of the language would be a nigh on impossible task. At least you’re asking questions about it so it’s learning even if it’s not strictly part of the lesson you’re on :smile:.

1 Like

Sure thing, you know how it is. 5 times you let it slip thinking ‘must have missed that bit’, and then the 6th time you think ‘this is 1 too many’

2 Likes