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
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304

1 Answers1

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.

Community
  • 1
  • 1
Hari Menon
  • 33,649
  • 14
  • 85
  • 108