I'm giving a talk at the U. of Toronto tomorrow, and would like to re-run the "what does it do?" experiment with list comprehensions and simultaneous loops. I propose asking the following: The program: for x in [10, 20, 30]: print x prints: 10 20 30 and the program: for x in [10, 20, 30]: for y in [1, 2, 3]: print x+y prints: 11 12 13 21 22 23 31 32 33 Match each of the following: (A) for x in [10, 20, 30]; y in [1, 2, 3]: print x+y (B) for (x,y) in zip([10, 20, 30], [1, 2, 3]): print x+y (C) for (x in [10, 20, 30]) and (y in [1, 2, 3]): print x+y (D) something else to their output: (1) 11 22 33 (2) 11 12 13 21 22 23 31 32 33 (3) "Run-Time Error" Questions: 1. What should option (D) be? 2. Should the lists be the same length, or different lengths? I think the latter is the trickier case (equally reasonable to say "iterate for length of shortest" and "error if lists' lengths unequal") --- is that worth testing? 3. Can someone provide a couple of list comprehension alternatives as well? Reactions? Greg
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4