Why can't I do this:
d = [x for x in range(7)]
a, b, c, d, e, f, g = *d
Where is it possible to unpack? Only between parentheses of a function?
Why can't I do this:
d = [x for x in range(7)]
a, b, c, d, e, f, g = *d
Where is it possible to unpack? Only between parentheses of a function?
You're using Extended Iterable Unpacking
in wrong way.
d = [x for x in range(7)]
a, b, c, d, e, f, g = d
print(a, b, c, d, e, f, g)
Where it's possible to unpack? Only between parentheses of a function?
No,
*
proposes a change to iterable unpacking syntax, allowing to specify a "catch-all" name which will be assigned a list of all items not assigned to a "regular" name.
You can try something like this:
a, *params = d
print(params)
Output
[1, 2, 3, 4, 5, 6]
Usually *
(Extended Iterable Unpacking) operator is used when you need to pass parameters to a function.
Note
Javascript equivalent of Extended Iterable Unpacking operator
is called spread syntax.
d = [...Array(7).keys()]
console.log(d)
var [a, ...b] = d
console.log(a,b)
d
and rest of the values will be packed to rest
variable. –
Bluefarb You can also use this:
>>> a,b,c,d,e,f,g = range(7)
>>> a
0
>>> b
1
>>> c
2
You don't appear to need the *
>>> z = [x for x in range(7)]
>>> a,b,c,d,e,f,g = z
>>> a
0
>>> b
1
>>> c
2
>>>
(I've used z rather than d twice.)
a,b,c,d,e,f,g = range(7)
works because unpacking works on iterables. –
Burgher © 2022 - 2024 — McMap. All rights reserved.