Loading topic
for loop over range(), you know what a list is, and you can call functions and methods such as str.upper(). It covers the parts of a list comprehension, how Python evaluates them, and the syntax errors you are most likely to hit.3 min read[expression for item in iterable], and that you know comparison operators and truthiness. Here you will add a filter so the comprehension keeps only some items.2 min readif filter, and read boolean expressions and comparison operators. It covers the second place an if can appear in a comprehension: the output expression.2 min readfor loop over range and over a list. It also assumes you know how to read and assign dictionary entries, and that you have seen list comprehensions in the earlier chapters. A dict comprehension uses the same pattern, but each item produces a key and a value i3 min readrange, the built-in list, dict, and set types, basic comparisons, and how to call append. It also assumes you have seen list comprehensions and the trailing if filter from earlier chapters.3 min readif filter, and that you know how nested for loops work. You will learn how a comprehension with several for clauses maps onto nested loops, and how to use that to flatten nested data.2 min readfor loop behaves. You should also know the difference between a local and a global variable. Here you will see where a comprehension keeps its loop variable and how that differs from a for loop.3 min readfor, range, and sum do. A generator expression looks almost the same as a list comprehension, but it behaves differently in memory and in how many times you can use it.3 min readif filter, and basic nested loops. Each pitfall below shows the symptom, the cause, and the fix.3 min readif filters, and that you know how a comprehension scopes its loop variable. It also assumes Python 3.8 or later.3 min readif filters and conditional expressions, and read nested for clauses. It covers how to decide when a comprehension helps and when a plain loop is the better choice.2 min read