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.

Author ned.deily
Recipients ned.deily, wilsoncampusano
Date 2014-07-26.02:46:40
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1406342801.48.0.219775956564.issue22075@psf.upfronthosting.co.za>
In-reply-to
Content
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
2014-07-26 02:46:41ned.deilysetrecipients: + ned.deily, wilsoncampusano
2014-07-26 02:46:41ned.deilysetmessageid: <1406342801.48.0.219775956564.issue22075@psf.upfronthosting.co.za>
2014-07-26 02:46:41ned.deilylinkissue22075 messages
2014-07-26 02:46:40ned.deilycreate