Learning outcomes
By the end of this lecture, you should be able to:- write list comprehensions correctly
- translate loop logic to comprehension form
- use conditional filtering in comprehensions
- avoid common syntax confusion
Basic form
With condition
Equivalent loop
Comprehension:String-based example
Conditional expression inside comprehension
Exam hints and traps
- condition comes after
forin filter form - comprehension builds a new list
- do not confuse with generator syntax
()and tuple syntax
Quick practice
- Build list of cubes from
1to5. - Build list of vowels from
"education". - Write loop equivalent of:
