0

This function prints multiple values, which are indices of a string z, however i want to store those values, and using return will terminate the function leaving me with only the first value of several.

def find(a):
    index=a
    while index<len(z)-1:
        if z[index]=="T":
            for index in range (index+20,index+30):
                if z[index]=="A" and z[index+1]=="G" and z[index+2]=="T":
                    a=index
                    print a
        index=index+1
2
  • 1
    Perhaps use yield instead of print Commented Oct 3, 2013 at 12:04
  • @user2842500: something you might want to read: python.org/dev/peps/pep-0008 Commented Oct 3, 2013 at 12:12

1 Answer 1

1

The most straightforward way is to return a tuple or list:

def find(a):
    index=a
    ret = []
    while index<len(z)-1:
        if z[index]=="T":
            for index in range (index+20,index+30):
                if z[index]=="A" and z[index+1]=="G" and z[index+2]=="T":
                    a=index
                    ret.append(a)
        index=index+1
    return ret

You can also use yield. I am removing the code for it (you can read the link, it is excellent) because I think returning a listmakes more sense in your case than yield. yield makes more sense if you don't intend to always use all of the values returned or if the return values are too many to hold in memory.

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.