Make python for loop go only once
<p>Alright, so I'm trying to make a Facebook bot over here to do some stuff for me, But I don't really think that matters for you.</p>

<p>Anyway, in order to achieve what I want to do I need to do some stuff. So, using the Facebook API I am getting some posts id with the following code:</p>

<pre><code>for posts in parsed_json:
    post_id = posts.get('id')
    post_url = "http://facebook.com/" + str(post_id)
    text_save(post_url)
</code></pre>

<p>But the problem is that this code gets me the last 25 post ID's and I only need the last one. 
So bassicaly what I'm trying to do is: Get the last post ID and then execute the text_save() function with that.</p>

<p>But that loop gets me 25 ids, and I don't need them. I need only the first one.</p>

<p>So how do I limit the for loop to run only once? I tried the following thing:</p>

<pre><code>a = 0
while a &lt; 1:
    for posts in parsed_json:
        post_id = posts.get('id')
        post_url = "http://facebook.com/" + str(post_id)
        text_save(post_url)
        a = a + 1
</code></pre>

<p>But that didn't really work out, it still goes through it 25 times. Any ideeas?</p>

<p>To get the last value (or first), simply use <code>"http://facebook.com/" + str(parsed_json[-1].get('id'))</code> (or parsed_json[0])</p>

<p>If you want to use loop instead,
to save just the last value, iterate and run the command afterwards:</p>

<pre><code>post_url = ''
for posts in parsed_json:
    post_id = posts.get('id')
    post_url = "http://facebook.com/" + str(post_id)
text_save(post_url)
</code></pre>

<p>To break from a loop after one interation use:</p>

<pre><code>a = 0
for posts in parsed_json:
    if a &gt;= 1: break
    post_id = posts.get('id')
    post_url = "http://facebook.com/" + str(post_id)
    text_save(post_url)
    a += 1
</code></pre>

python facebook