Welcome to the Wikipedia Computing Reference Desk Archives |
---|
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
May 15
I'm having trouble finding a reference for the proper terminology of two kinds of for loops in Python. The first one is the normal: for x in a: x+4 (I know this doesn't do anything, it is for this example) The second one is kind of backwards: x+4 for x in a What is the name of the for loop when the calculation comes before the "for" keyword? 75.136.148.8 (talk) 14:15, 15 May 2024 (UTC)[reply]
- The second one is a list comprehension, not a for loop: https://docs.python.org/3/tutorial/datastructures.html#tut-listcomps Variouspotatoes (talk) 17:24, 15 May 2024 (UTC)[reply]
- Thanks. I didn't think to jump to the data structures section of the documentation to look for it. 75.136.148.8 (talk) 18:21, 15 May 2024 (UTC)[reply]
- It is only a list comprehension when enclosed between square brackets. Enclosed in curly braces, it becomes a set comprehension. --Lambiam 14:08, 16 May 2024 (UTC)[reply]
- And in round parentheses a generator expression. --Wrongfilter (talk) 15:12, 16 May 2024 (UTC)[reply]