How do I make a function iterate over a list twice, picking up where it left off?
<p>I'm making a Facebook Messenger bot which returns the times of buses coming to a specified stop. So far, the bot is working fine, but Facebook doesn't allow for messages sent by bots to be over 320 characters. Often, the bot can only display the first 5 or so without going over this limit, which isn't good enough at very busy stops.</p>

<p>I have an <code>if</code> statement in place which only shows the first five results if the stop has more than that, and then passes the results to the bot's <code>send_message</code> function.</p>

<p>I'm looking for a way to get the results from large stop lists in separate chunks of 5, and have the bot pick up where it left off, after sending the first message. My current code is as follows:</p>

<pre><code>if len(info["results"]) &gt; 5:
    while i &lt; 5:
        n.append("Route:" + " " + str(info['results'][i]['route']) + " " + "to" + " " + str(info['results'][i]['destination']) + "\n" + "Due:" + " " + str(info["results"][i]["duetime"]) + " " + "minutes." + "\n")
        i = i + 1
else:
    while i &lt; len(info["results"]):
        n.append("Route:" + " " + str(info['results'][i]['route']) + " " + "to" + " " + str(info['results'][i]['destination']) + "\n" + "Due:" + " " + str(info["results"][i]["duetime"]) + " " + "minutes." + "\n")
        i = i + 1

return '\n'.join(str(x) for x in n)
</code></pre>

<p>The <code>return</code> statement at the bottom is what is getting passed to the <code>send_message</code> function. Is there any way I can achieve the multiple message method?</p>

<p>Here is a sample code which makes a list of lists which contains info about 5 stops. Then, you can call <code>send_message</code> in a loop</p>

<pre><code>def split_into_pairs_of_5(info)
    outer = []
    inner = []
    for x in range(len(info["results"])):
        if x % 5 == 0 and inner:
            outer.append(inner)
            inner = []

        inner.append("Route:" + " " + str(info['results'][i]['route']) + " " + "to" + " " + str(info['results'][i]['destination']) + "\n" + "Due:" + " " + str(info["results"][i]["duetime"]) + " " + "minutes." + "\n")

    if inner:
        outer.append(inner)
        inner = []

    return outer


result = split_into_pairs_of_5(info)

for i in info:
    send_message('\n'.join(str(x) for x in i))
</code></pre>

python