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: Lambda, Enumerate and List comprehensions crash
Type: crash Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ned.deily, wilsoncampusano
Priority: normal Keywords:

Created on 2014-07-26 02:00 by wilsoncampusano, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_c.py wilsoncampusano, 2014-07-26 02:00 code that crash
test_c.py wilsoncampusano, 2014-07-26 02:15 Code that crash
Messages (3)
msg224015 - (view) Author: wilson campusano (wilsoncampusano) * Date: 2014-07-26 02:00
SO: Ubuntu 14.04

When i run this code my pc crash
msg224017 - (view) Author: wilson campusano (wilsoncampusano) * Date: 2014-07-26 02:15
SO: Ubuntu 14.04

When i run this code my pc crash
msg224018 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2014-07-26 02:46
It may be hard to see what's going on with the code written as a list comprehension.  We could expand it out to something roughly equivalent and print the first n iterations:

def main():
    lista =[1, 4, 5 , 5, 6 , 3 ,1]
    def ins(x):
        return lista.insert(x,0)

    for idx, v in enumerate(lista):
        if v == 5:
            ins(idx)
            print(idx, lista)
        if idx > 10:
            break

if __name__ == '__main__':
	main()


(2, [1, 4, 0, 5, 5, 6, 3, 1])
(3, [1, 4, 0, 0, 5, 5, 6, 3, 1])
(4, [1, 4, 0, 0, 0, 5, 5, 6, 3, 1])
(5, [1, 4, 0, 0, 0, 0, 5, 5, 6, 3, 1])
(6, [1, 4, 0, 0, 0, 0, 0, 5, 5, 6, 3, 1])
(7, [1, 4, 0, 0, 0, 0, 0, 0, 5, 5, 6, 3, 1])
(8, [1, 4, 0, 0, 0, 0, 0, 0, 0, 5, 5, 6, 3, 1])
(9, [1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 6, 3, 1])
(10, [1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 6, 3, 1])
(11, [1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 6, 3, 1])

Because the list is mutating by inserting the 0 before the 5, once the 5 entry is found, it keeps "moving" to the right so the loop never terminates and lista keeps expanding until Python runs out of memory.  Don't do that!
History
Date User Action Args
2022-04-11 14:58:06adminsetgithub: 66273
2014-07-26 02:46:41ned.deilysetstatus: open -> closed

nosy: + ned.deily
messages: + msg224018

resolution: not a bug
stage: resolved
2014-07-26 02:15:00wilsoncampusanosetfiles: + test_c.py

messages: + msg224017
2014-07-26 02:11:22wilsoncampusanosettitle: Lambda, enumerate and list comprehensins crash -> Lambda, Enumerate and List comprehensions crash
2014-07-26 02:00:11wilsoncampusanocreate