I see some great posts on how to break Python lists into pieces, such as how to break iterability in constant-sized pieces . Most posts are about splitting chunks or combining all the lines in a list together, and then a restriction based on regular slice routines.
However, I needed to do something similar based on the character limit. If you have a list of suggestions, but cannot cut off any fragments in the list.
I managed to run the code here:
def _splicegen(maxchars, stringlist): """ Return a list of slices to print based on maxchars string-length boundary. """ count = 0
The result should return something like: Slices: [[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20]] (Each number refers to an item in a list of strings)
So, when I iterate over this list of lists, I can use something like "" .join ([item for item in each]) to print 0,1,2,3,4,5,6 on one line, 7, 8,9,10,11,12,13 on the other. Sometimes a list can consist of only two elements, because each of these two elements is very long (it can be up to 380 characters or something else).
I know that the code is pretty bad and that I should use a generator. I just don't know how to do this.
Thanks.