Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
Answer: C. [8, 12] Solution: a and b are lists. Then there is a list comprehension that gives c. Let's analyze the list comprehension. Here is the trick to analyze list comps: 🗝️ Look for 3 things: `for`, `in`, and `if` (`if` is optional, sometimes it may not be there) +
I'm showing clear separations of for, in, and if here: [ (x) for (x) in (a) if (x not in b) ] We look at what is between `for` and `if`. It is: for x in a This is just like a regular for-loop: for x in a: That already looks so much simpler. Let's go on. +
Now we have to look at what comes at `if` and beyond, it is: if (x not in b) This is the familiar if-form. Simply think of it as nested within the `for` loop, like so: for x in a: if (x not in b): Even more simplified, familiar structure! Now only one part is remaining +
United States 趨勢
- 1. #heatedrivalry 92K posts
- 2. Rams 79.5K posts
- 3. Seahawks 104K posts
- 4. ilya 83.1K posts
- 5. Sam Darnold 20.8K posts
- 6. Puka 53.2K posts
- 7. svetlana 7,759 posts
- 8. Stafford 22.3K posts
- 9. Al Michaels 2,905 posts
- 10. SCOTT HUNTER 12.5K posts
- 11. #PowerForce 1,503 posts
- 12. Rose 173K posts
- 13. Ben Shapiro 48.1K posts
- 14. #zzzSpecialProgram 6,711 posts
- 15. jacob tierney 6,132 posts
- 16. connor storrie 17.9K posts
- 17. #TSTheEndOfAnEra 11.1K posts
- 18. McVay 8,335 posts
- 19. #TNFonPrime 6,828 posts
- 20. Portuguese 31K posts
Something went wrong.
Something went wrong.