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: [list.append(i) for i in list] causes high resources usage
Type: resource usage Stage: resolved
Components: Interpreter Core Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Spencer Brown, serhiy.storchaka, txlbr
Priority: normal Keywords:

Created on 2021-10-22 18:55 by txlbr, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg404813 - (view) Author: TL (txlbr) Date: 2021-10-22 18:55
Version:
Python 3.10.0 - Windows 11 (22000.258) x64
Python 3.9.7 - Windows 11, Manjaro Linux 21.1.5 - Kernel 5.13.13-1-MANJARO x64
CPU: 8 × Intel® Core™ i5-8300H CPU @ 2.30GHz

Steps to reproduce:
1. Create any list, for example your_list = [1, 2, 3]
2. Execute [your_list.append(i) for i in your_list]
msg404834 - (view) Author: Spencer Brown (Spencer Brown) * Date: 2021-10-22 22:02
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).
msg404859 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-10-23 08:03
It is expected behavior. Your code is equivalent to:

_result = []
for i in your_list:
    _result.append(your_list.append(i))

which is equivalent to:

_result = []
_j = 0
while _j < len(your_list):
    i = your_list[_j]
    _result.append(your_list.append(i))
    _j += 1

It is an infinite loop (because len(your_list) is increased after your_list.append(i)), and two lists grow with every iteration.
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89742
2021-10-24 04:20:36rhettingersetstatus: open -> closed
resolution: not a bug
stage: resolved
2021-10-23 08:03:04serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg404859
2021-10-22 22:02:53Spencer Brownsetnosy: + Spencer Brown
messages: + msg404834
2021-10-22 18:55:25txlbrcreate