'My For Loop takes forever to get through a list, Is there any way to speed this up?
I'm trying to make a program that saves 4 Lists, but they each have 100+ strings in them. It takes forever to save all of them, is there a way to speed it up or a different way to do the same thing that is faster?
from replit import db
list1 = ['a','a','a','a','a','a','a','a','a','a','a'] # +500 more a's
list2 = ['a','a','a','a','a','a','a','a','a','a','a'] # +500 more a's
list3 = ['a','a','a','a','a','a','a','a','a','a','a'] # +500 more a's
list4 = ['a','a','a','a','a','a','a','a','a','a','a'] # +500 more a's
print('Saving...')
for i in list1:
db['savelist1'] += i
for i in list2:
db['savelist2'] += i
for i in list3:
db['savelist3'] += i
for i in list4:
db['savelist4'] += i
print('Finished Saving')
I tried making db['savelist'] equal the list so it did it instantaneously, but it turned into an ObservedList. Then I tried to append it but it gave an error saying I can't append a string.
Solution 1:[1]
This isn't perfect, but it should be a lot faster.
You don't need to loop at all.
db['savelist1'] += ''.join(list1)
db['savelist2'] += ''.join(list2)
db['savelist3'] += ''.join(list3)
db['savelist4'] += ''.join(list4)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Gordon |
