Build Arrays Fast with Comprehensions
Read and shape input in one line.
Build Lists in One Line
A list comprehension builds a list from a loop in a single expression. It is shorter, faster, and the contest standard for shaping data.
squares = [i * i for i in range(5)]The Basic Shape
The pattern is [expr for item in iterable]. The expression on the left runs once for each item the loop yields.
doubles = [x * 2 for x in nums]All lessons in this course
- Lists, Indexing & Slicing for CP
- Build Arrays Fast with Comprehensions
- Min, Max, Sum & Running Totals
- Find the Index, Not Just the Value