-3

I have to write a function which returns the every alternate element of a tuple using Python. For eg: if input is (1,"hi",2,"hello",5); my output should be (1,2,5). I got the answer using while loop and [::2]. But when i try for loop, i face error that tuple occupies 1 positional argument but input has 5 positional argument. So, can anyone give me the equivalent for loop function for the while loop function I am attaching? "https://pastebin.com/embed_js/YkGdiyva"

def oddTuples(aTup):
    '''
    aTup: a tuple

    returns: tuple, every other element of aTup. 
    '''
    # Your Code Here
    rTup = ()
    index = 0
    while index < len(aTup):
        rTup += (aTup[index],)
        index += 2

    return rTup
clemens
  • 16,716
  • 11
  • 50
  • 65
  • 3
    `[item for n, item in enumerate(my_tuple) if not n % 2]` Use a conditional list comprehension with enumeration. – Alexander Jan 03 '18 at 05:51
  • Thanks @Alexander, but I am very new to Python and doing the MITx course currently on Python. So, I am not very well acquainted with enumerate function. Can you shed some more light on it? – Arghyadeep Das Jan 03 '18 at 06:09
  • https://stackoverflow.com/questions/22171558/what-does-enumerate-mean – Alexander Jan 03 '18 at 06:10

3 Answers3

1

Try the below code:

def oddTuples(aTup):
    out=()
    for i in range(len(aTup)):
        if i%2==0:
            out = out + (aTup[i],)
return out
aTup=(1,"hi",2,"hello",5)
print oddTuples(aTup)

Output when I ran the above code:

(1, 2, 5)
Abhijit
  • 1,728
  • 1
  • 14
  • 25
0
def oddTuples(aTup):
    rTup = ()
    for i,t in enumerate(aTup):
        if i%2==0:
            rTup += (t,)

    return rTup
Sagun Shrestha
  • 1,188
  • 10
  • 23
  • what is this enumerate function? Kinda new for me as i am just into python, doing the MITx 6.00.1x course. – Arghyadeep Das Jan 03 '18 at 06:05
  • enumerate(var) will return a tuple (i,x) where i is the index and x is the ith element of var. var can be a list or tuple or any other iterables – Sagun Shrestha Jan 03 '18 at 06:07
0

You can use also use range to providse suitable indeces to access tuple values. range can take 1, 2, or 3 parameters.

If one parameter is feed into range, say, range(5), it will generate a sequence of integers starting from 0 and stops at 5. (5 will be excluded so only 0, 1, 2, 3, 4 will be given.)

If two parameters are feeded, say range(3, 5), 3 is the start index and 5 is the index that it stops. Note again 5 is excluded.

If takeing three numbers, like range(0, 10, 2), the first parameter is the index to start; the second is the ending index, and the third is the step size. We demonstrate two ways below.

def oddTuples(aTup):
    rTup = ()
    for i in range(len(aTup)):  # will loop over 0, 1, ..., len(aTup)-1
        if i % 2 == 0:          # a condition to filter out values not needed
            rTup += (aTup[i],)
    return rTup

def oddTuples(aTup):
    rTup = ()
    for i in range(0, len(aTup), 2):  # the 2 is to specify the step size
        rTup += (aTup[i],)            # 0, 2, 4, ... till the sequence is exausted.
    return rTup
Tai
  • 7,684
  • 3
  • 29
  • 49