"Dinesh B Vadhia" <dineshbvad...@hotmail.com> wrote

This was discussed in a previous post but I didn't see a solution. Say, you have

for i in veryLongListOfStringValues:
    s += i

In general avoid string addition.
(Although recent versions of Python have optimised it somewhat)

As s gets very large, how do you deal with this situation to avoid
a memory error or what I think will be a general slowing down
of the system if the for-loop is repeated a large number of times.

Avoid string addition.
join() is one way, a format string might also work - but I haven't
benchmarked that for memory or speed

fmt = "%s" * len(biglist)
s = fmt % tuple(biglist)

Just some ideas...


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to