Can someone help me to walk through the logic behind the syntax for list comprehension using the example before exercise? I have hard time understanding why we need to enter temporary variable
word in front of the for loop?
words = ["@coolguy35", "#nofilter", "@kewldawg54", "reply", "timestamp", "@matchamom", "follow", "#updog"] usernames = [**word** for word in words if word == '@']
Per example, it says that this list comprehension:
- Takes an element in
words-> I I understand that this happens via the for loop:
for word in words
- Assigns that element to a variable called
word-> I understand that this happens with every for loop when it assigns the value of the element it has found in the list to a temporary variable that we call here
This is where I get stuck as I do not understand why we have to write
word again in code before the for loop? What program “sees” when it hits this and why it can not run the code without it?
Thank you for the logic behind it!