[Tutor] large strings and garbage collection
Alan Gauld
alan.gauld at btinternet.com
Sat Jul 18 00:32:12 CEST 2009
"Dinesh B Vadhia" <dineshbvadhia at 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/
More information about the Tutor
mailing list