How do I return the value from each iteration of a loop to the function it is running within?
<p>I'm making a program that scrapes local bus times from a real time information server and prints them. In order to return all the bus times, it does this:</p>

<pre><code>while i &lt; len(info["results"]):
    print "Route Number:" + " " + info['results'][i]['route']
    print "Due in" + " " + info["results"][i]["duetime"] + " " + "minutes." + "\n"
    i = i + 1
</code></pre>

<p>This works fine, and returns all of the results, one by one like so:</p>

<p>Route Number: 83
Due in 12 minutes.</p>

<p>Route Number: 83
Due in 25 minutes.</p>

<p>Route Number: 83A
Due in 39 minutes.</p>

<p>Route Number: 83
Due in 55 minutes.</p>

<p>However, as I'm using this feature within another script, I turned the code to fetch times and return them into a function:</p>

<pre><code>def fetchtime(stopnum):
    data = "?stopid={}".format(stopnum)+"&amp;format=json"
    content = "https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation"
    req = urllib2.urlopen(content + data + "?")

    i = 0 
    info = json.load(req)

    if len(info["results"]) == 0:
        return "Sorry, there's no real time info for this stop!"

    while i &lt; len(info["results"]):
        return "Route Number:" + " " + str(info['results'][i]['route']) + "\n" + "Due in" + " " + str(info["results"][i]["duetime"]) + " " + "minutes." + "\n"
        i = i + 1 
</code></pre>

<p>This works, however it only returns the first bus from the list given by the server, instead of however many buses there may be. How do I get the printed result of the function to return the info supplied in each iteration of the loop?</p>

<p>I would suggest you to use the yield statement instead return in fetchtime function.</p>

<p>Something like:</p>

<pre><code>def fetchtime(stopnum):
    data = "?stopid={}".format(stopnum)+"&amp;format=json"
    content = "https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation"
    req = urllib2.urlopen(content + data + "?")

    i = 0 
    info = json.load(req)

    if len(info["results"]) == 0:
        yield "Sorry, there's no real time info for this stop!"

    while i &lt; len(info["results"]):
        yield "Route Number:" + " " + str(info['results'][i]['route']) + "\n" + "Due in" + " " + str(info["results"][i]["duetime"]) + " " + "minutes." + "\n"
        i = i + 1 
</code></pre>

<p>It would allow you to pick one data at a time and proceed.</p>

<p>Lets say that info["results"] is a list of length 2, then you could do:</p>

<pre><code>&gt;&gt; a = fetchtime(data)
&gt;&gt; next(a)
Route Number: 83 Due in 25 minutes.
&gt;&gt; next(a) 
Route Number: 42 Due in 33 minutes.
&gt;&gt; next(a)
StopIteration Error
or simple do:
&gt;&gt; for each in a:
    print(each)
Route Number: 83 Due in 25 minutes.
Route Number: 42 Due in 33 minutes.
# In case if there would be no results (list would be empty), iterating 
# over "a" would result in:
&gt;&gt; for each in a:
    print(each)
Sorry, there's no real time info for this stop!
</code></pre>

python json function loops