2

My function prints the the index of every element in list that is divided by 2 in a given list. I want to know if there is a way to return all the indexes rather than printing?

def printEvenIndex(referenceTuple):                     

    for i in referenceTuple:
        if i % 2 ==0:
            indexOfEvenIntegers = referenceTuple.index(i)
            print(indexOfEvenIntegers)
    return indexOfEvenIntegers 



referenceTuple = (6,5,3,4,1)
print(printEvenIndex(referenceTuple))

Right now print statement prints 0 ,3 which is valid. But return function is returning 3 only. Is there a way to tell return function to return every element that is divisible by 2? I want to return all the index rather than print it.

1 Answer 1

3

Just create a list and append your indexes there:

def readEvenIndexes(referenceTuple):  
    """ Named it readEventIndexes, as we are not printing anymore """                   
    indexes = []
    for index, i in enumerate(referenceTuple):
        if i % 2 ==0:
            indexes.append(index)

    return indexes 


referenceTuple = (6,5,3,4,1)
print(readEvenIndexes(referenceTuple))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.