List comprehensions


#1

just curious about shorthand for list comprehensions.
in the example it says:
usernames = [ word for word in words if word[0] == ‘@’]
why the first ‘word’ in that sequence? what does it do?


#2

word is the block parameter of the for…in loop. If it meets the condition in the loop block, it gets written to the output list. That is what the first word is doing.

[append_item_to_list for ... if ...]

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.