[issue45579] [list.append(i) for i in list] causes high resources usage

Spencer Brown report at bugs.python.org
Fri Oct 22 18:02:53 EDT 2021


Spencer Brown <spencerb21 at live.com> added the comment:

This is intentional behaviour, you actually created an infinite loop. When you iterate over a list, all Python does is track the current index through the list internally, incrementing it each time. But each iteration, you call list.append() to add a new item to the end of the list, so you're continually making it longer and preventing the iteration from ending.

Regardless of that, this probably isn't a good use of list comprehensions anyway - append() always returns None, so the result of this comprehension would be a useless list of Nones. It'd be better to just use a regular for loop, or if you're just wanting to copy the list call list.copy() or list(your_list).

----------
nosy: +Spencer Brown

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45579>
_______________________________________


More information about the Python-bugs-list mailing list