This code will load 10000 instances of strings (with same content):
lst = []
for i in xrange(0, 10000):
with open ('data.txt', 'r') as datafile:
lst.append(str(datafile.read()))
print(lst)
Only by adding code after or before the above one, i wan't to have the same result as this.
lst = []
with open ('data.txt', 'r') as datafile:
s = str(datafile.read())
for i in xrange(0, 10000):
lst.append(s)
print lst
This will load only 1 instance of string. => Less memory usage.
In java, there is String.intern() ? I look for a python equivalent.