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>

<p>Here is an example you can use to finish the program.</p>

<pre><code>myTup = (1,2,3,4,5,6,7,8)

for i in range(len(myTup)):
    if i%2 != 0:
        print("Tuple items: " ,myTup[i])
        print("i here is: " , i)
</code></pre>

<p><strong>len</strong> -> gives you the length of a object ; eg. if you say len(myTup). That will be 8</p>

<p><strong>Range</strong> -> is like a range, (number of elements) you will be iterating over. </p>

<p><strong>i</strong> -> is the index. </p>

<p>Here is the output </p>

<pre><code>Tuple items:  2
i here is:  1
Tuple items:  4
i here is:  3
Tuple items:  6
i here is:  5
Tuple items:  8
i here is:  7
</code></pre>

<p>You can get the same output above by running the code without using range and len.</p>

<pre><code>for i in myTup:
    if i%2 != 0:
        print("Tuple items: " ,myTup[i])
        print("i here is: " , i)
</code></pre>

python for-loop range iteration tuples