difference between range / len etc. when iterating over tuples
<p>One thing upfront: I am fairly now to the coding world so maybe my question is a bit stupid ... I was trying to write a function that returns the every other element of a tuple. The easiest way obviously is </p>

<pre><code>def oddTuples(aTup):
     return aTup[::2]
</code></pre>

<p>I tried to solve it differently by using the following code</p>

<pre><code>def oddTuples(aTup):
    newTup = ()
    for i in len(aTup):
        if i%2 != 0:
            newTup = newTup + (i,)
    return newTup
</code></pre>

<p>But that doesn't give me back anything at all.</p>

<p>I thought I (if used over <code>len</code>) gives back the position, so if <code>aTup = ((12, 34, 'abc', 'dfdf', 2340))</code> the return would be <code>newTup = ((12, 'abc', 2340))</code>.</p>

<p>What's the <code>i</code> iterating over when used with <code>range</code>, <code>len</code> or -in that case- while iterating over <code>for i in aTup:</code>?</p>

<pre><code>newTup = ()
b = True
for i in aTup:
    if b:
        newTup = newTup + (i,)
    b = not b
return newTup
</code></pre>

<p>Try that on for size.  The <code>for</code> statement gives us the tuple values one by one, and the <code>boolean</code> b lets us skip every other one.  </p>

<p>The best way to do this is the way you put at the top of your post. If you don't understand what it's doing, then I suggest researching list slicing.</p>

python for-loop range iteration tuples