4 ms·
Just a quick FYI, but in vanilla Python 3 [1] you can already do a head/tail split on any iterable: >>> hd, *tl = range(5) >>> hd 0 >>> tl
by lou1306 8y ago
Just a quick FYI, but in vanilla Python 3 [1] you can already do a head/tail split on any iterable:
>>> hd, *tl = range(5)
>>> hd
0
>>> tl
[1, 2, 3, 4]
[1] Tested on 3.6.5, don't know about older versions
- quietbritishjim 8y agoThis is due to PEP 448 "Additional Unpacking Generalisations" [1], which was approved with effect from Python 3.5. Other examples: x, y, *r, z = range(7) print("x: {}, y: {}, r: {}, z: {}".format(x, y, r, z)) # prints: x: 0, y: 1, r: [2, 3, 4, 5], z: 6 x, (qa, *qr), *r = [1, [2, 3, 4], 5, 6] print("x: {}, qa: {}, qr: {}, r: {}".format(x, qa, qr, r)) # prints: x: 1, qa: 2, qr: [3, 4], r: [5, 6] The same PEP allows expanding lists and dictionaries in literals: r = [*r1, *r2] # Same as r = r1 + r2 if they are lists d = {**d1, **d2} # Merges d1 and d2 into d [1] https://www.python.org/dev/peps/pep-0448/ https://www.python.org/dev/peps/pep-0448/
- quietbritishjim 8y agoI was mistaken, sorry. Those unpackings work as of Python 3.0 due to PEP 3132 "Extended Iterable Unpacking" [2]. PEP 448 is just for those examples in the second half of my comment. [2] https://www.python.org/dev/peps/pep-3132/ https://www.python.org/dev/peps/pep-3132/