This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Misleading information about slice assignment in docs
Type: enhancement Stage:
Components: Documentation Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, ezio.melotti, stefanchrobot, terry.reedy
Priority: normal Keywords:

Created on 2013-05-08 21:31 by stefanchrobot, last changed 2022-04-11 14:57 by admin.

Messages (2)
msg188738 - (view) Author: Stefan Chrobot (stefanchrobot) Date: 2013-05-08 21:31
http://docs.python.org/3/reference/simple_stmts.html#assignment-statements

The docs says:

"If the target is a slicing: The primary expression in the reference is evaluated. It should yield a mutable sequence object (such as a list). The assigned object should be a sequence object of the same type."

This seems wrong, because the assigned object can be any iterable:

a = [4, 5, 6]
a[0:0] = range(1, 4)
# a is now [1, 2, 3, 4, 5, 6]
msg188870 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-05-10 19:45
Since range objects have a known length, that example is not enough to show 'any iterable'. However, generators do not even have a __length_hint__ and they work too.

a = [1,2,3]
a[0:1] = (i for i in range(4))
print(a)
>>> 
[0, 1, 2, 3, 2, 3]
History
Date User Action Args
2022-04-11 14:57:45adminsetgithub: 62139
2020-11-17 15:23:56iritkatrielsetversions: + Python 3.8, Python 3.9, Python 3.10, - Python 3.3
2013-05-10 19:45:39terry.reedysetnosy: + terry.reedy
messages: + msg188870
2013-05-08 21:48:01ezio.melottisetnosy: + ezio.melotti
2013-05-08 21:31:39stefanchrobotcreate