Need more info on this line of code

Hello!
I am just curious on how this shortcut of for loop is executed. I am not sure if it is covered in the previous courses, any information to help me understand the reasoning behind it would be helpful. If there is a link to help me learn how to apply this shortcut would be even better.
Thank you

yearmonths = [y*100+m for y in years for m in months]

The shortcut is known as List Comprehension. Learn more from this blog: Python Basics: List Comprehensions to get a clearer understanding of List Comprehensions

1 Like

Just complementing what @info.victoromondi just said, this:

yearmonths = [y*100+m for y in years for m in months]

is the same as this:

yearmonths = []
for y in years:
   for m in months:
        yearmonths.append(y * 100 + m)

It is just a more concise way to create a list.

3 Likes