-1

I'm learning python and I love that you can loop through iterable objects without constantly creating index variables.

But I keep finding myself creating index variables anyway so I can reference them in calls from parallel objects. Like when I'm comparing two lists, or a list and a dictionary.

As requested: some examples

`s= "GAGCCTACTAACGGGAT"# the strings we are evaluating
t= "CATCGTAATGACGGCCT"

c = 0# c = position of interest within the strings
m = 0 # m = number of mutations found so far. 

for i in s: # cycling through all nucleotides listed in s
if(i == t[c]): #compare s[c] to t[c]
    c +=1 # if equal, increase position counter
else:
    c+=1 # if not equal, increase both position and                 
    m+=1        #mutation counters.`

AND

def allPossibleSubStr(s): #takes a dict of strings, finds the shortest, and produces a list of all possible substrings, thus a list of possible common substrings to all elements of the original dict
ks = s.keys()
c=0 #counter
j=0 # ks place holder for shortest string
subSTR = []

for i in ks: #finds the shortest entry in stringDict
    if(s[i] < s[ks[(c+1)%len(s)]]):
        j=c
    c +=1

c=s[ks[j]] #c is now a string, the shortest... 
j=ks[j] # j is now a key string, the shortest...
n = (len(c)*(len(c)+1))/2 # number of subsets of c

#producing a list of possible substrings
for i in range(len(c)):
    for k in range(len(c)-i):
        subSTR.append(c[i:i+k+1])
        #print("i =" +str(i)+ " and k=" + str(k))
#is there a list function with eleminates duplicate entries. 
subSTR=list(set(subSTR))# a set does not have any duplicate entires
subSTR.sort(key=len) # sorts substring from shortest to longest string
subSTR.reverse()

return subSTR

Is there a way around this?

Carl
  • 41
  • 9
  • 1
    care to give an example of what you mean? – Tasos Papastylianou Aug 13 '16 at 22:56
  • 2
    Please show an example of what you need to use the indexes with. Depending on your use case, there are likely other ways. E.g. if you have two lists that you need to iterate at the same time, you could use `zip()`. – poke Aug 13 '16 at 22:58
  • I just had a quick look over more than 1000 for loops in multiple python projects of mine, and less than 10 use indexes (with `enumerate`). So yeah, most probably you're overlooking some better way to express your intent. – spectras Aug 13 '16 at 23:04

2 Answers2

2

Use enumerate...

for i, item in enumerate(myList):
  foo = parallelList[i]

or zip...

for item, foo in zip(myList, parallelList):
  ...
Blake O'Hare
  • 1,863
  • 12
  • 16
2

It sounds like you write code like this:

for i, item in enumerate(list1):
    if item == list2[i]:
        ...

You still don't need an explicit index, because you can zip the two lists and iterate over a list of tuples.

for item1, item2 in zip(list1, list2):
    if item1 == item2:

You can do the same thing with two dicts, although since they are unordered, you probably want to zip their keys after sorting:

for key1, key2 in zip(sorted(dict1), sorted(dict2)):
chepner
  • 497,756
  • 71
  • 530
  • 681