Skip to content
Advertisement

String formatting

I have a list filter = [‘a’, ‘b’, ‘c’]. I need to frame the following string out of the list “item -a item -b item -c”. Which is the most efficient way to do this? Usually the list filter contains 100 to 200 items and each would be of length 100 – 150. Wouldn’t that lead to overflow? And what is the maximum length of the string supported?

Advertisement

Answer

A cleaner way to do this:

filter = ['a', 'b', 'c']
" ".join(["item -%s" % val for val in filter])

This works fine with large arrays, eg. filter = ['a'*1000] * 1000.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement