I've got a list of words (domains in my case) and I need to split this list into groups, each group should contain not more than N chars (=bytes). One important thing is that the last word in each group should not break in the middle. I mean there should not be situations like:
google.com
yahoo.com
bin
In my code I only managed to retrieve the first group, I don't know how to make a proper loop to split the list into multiple chunks.
domains = ['google.com','yahoo.com','bing.com','microsoft.com','apple.com','amazon.com']
domains = list(domains)
text = ""
chars_sent=0
max_chars=20
for each_domain in domains:
if chars_sent > max_chars:
chars_sent = 0
break
text += each_domain+"\n"
chars_sent += len((str(each_domain)))
print(text)
Expected output:
google.com
yahoo.com <--- 19 chars in this part
bing.com <--- 8 chars in this part
microsoft.com <--- 13 chars in this part
apple.com
amazon.com <--- 19 chars in this part