Spreading into a list ([*a, *b])
A * inside square brackets copies the items of another iterable, one by one, into the new list.
Used in 19 lessons of the course, from 00.04.
Inside a list literal, *something means: "take each item of something and put it here". Python builds a new list with those items, in order.
You can mix single items with spreads: [0, *a, 9, *b]. Without the *, a would go in as one element, a list inside the list.
It works with any iterable: lists, tuples, sets, ranges, strings, or the result of .parents. That makes it a short way to say "this value first, then everything from that collection".
In the course you see it like this: [Path.cwd(), *Path.cwd().parents]. It is a list that starts with the current folder and continues with all its parent folders. It is written this way because .parents is not a list, so you can't add it with +.
[0, *range(3)] # [0, 0, 1, 2]
Reach for it when: you want a new list made of a single value plus the items of another iterable, or when joining several collections.
Examples
Common mistake
a = [1, 2] print([0, a])
Why: Without the *, `a` goes in whole as a single element, leaving a list inside a list.
How to write it: Write [0, *a] to get [0, 1, 2].
Common mistake
n = 5 print([0, *n])
Why: What follows the * must be an iterable, and an int can't be iterated.
How to write it: Spread something iterable, or put the value in without *: [0, n].
