Memory limit exceeded??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Memory limit exceeded???

aList = [123, 'xyz', 'zara', 'abc']; for x in aList: aList.append(x) print (x) print (aList) Above is the code. Why it shows me memory limit exceeded???

31st Oct 2018, 10:41 PM
livperis
livperis - avatar
7 Answers
+ 2
I'm no Python expert, but livperis, if you just wanna copy aList to itself _once_ then: aList.append(aList) should work as Python is very abstract. I believe this may work too, even: aList = aList + aList If you _need_ to copy _element-by-element_, I guess: bList = [] bList.append(aList); for ii in aList: bList.append(ii) Note: Python provides iterators and other stuff, iirc, so there's a way to do it like a boss probably, I just don't recall Python enough to recall it. * Honestly, I barely recall _any_ Python.
1st Nov 2018, 6:10 PM
non
+ 1
Because it infinitely keeps adding to the list. Each element found, another is appended as it iterates through aList. Thus aList keeps growing, it keeps iterating, it keeps growing... forever or until terminated. What are you tryna do?
31st Oct 2018, 10:56 PM
non
+ 1
You appear to be modifying the list over which you're iterating. At what point does it stop?
31st Oct 2018, 10:57 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Thank you you were very helpful to me.
1st Nov 2018, 6:37 PM
livperis
livperis - avatar
0
Thank you very much.
1st Nov 2018, 4:48 AM
livperis
livperis - avatar
0
I am trying to copy the items of the aList 123, 'xyz', 'zara', 'abc' once again in aList.
1st Nov 2018, 4:59 AM
livperis
livperis - avatar
0
Cool, NP :)
2nd Nov 2018, 1:20 AM
non