Skip to main content

← All terms Β· Data structures

List comprehension

Inline syntax for building a list from another iterable, with optional filter: `[f(x) for x in xs if cond(x)]`. Beats `map+filter+lambda`.

[x*2 for x in nums if x > 0]

Learn this interactively:

Open lesson lesson-66 β†’

Related β€” Data structures

Dict comprehensionSame as list comp, but builds a dict: `{k: v for k, v in pairs}`.TupleOrdered immutable sequence. Use for fixed-size records and as dict keys.SetUnordered collection of unique hashable values. O(1) membership check.DictionaryHash map from keys to values. Insertion order preserved since Python 3.7.IterableAnything you can loop over with `for`. Lists, tuples, sets, dicts, strings, file…GeneratorA function with `yield`. Produces values lazily, one at a time β€” uses constant m…