for - else vs for elif
for-loop, if-statement, python, python-2.7
Solution
`elif:` isn't a macro that expands to `else: if`, it is a syntactical element that is valid only in the context of an `if:` statement. Ordinarily, `else: if` would open a new `if` block; however, `elif:` doesn't do that.
Problem
i thought that elif: was the shorthand for ``` else: if: ``` but it's not possible to use for - elif: only for - else: if: in this code: ``` for line in source: change_next = False for dataset,artnr,revision in datasets: if dataset in line: change_next = True print " ** " + dataset + " found" datasets.remove((dataset,artnr,revision)) break else: if line.startswith("DstID:"): print line.replace("DstID:","").rstrip() if change_next and "Partno:" in line: destination.write("Partno: " + artnr + "\n") print "Partno: " + artnr elif change_next and "Revno:" in line: destination.write("Revno:" + revision + "\n") print "Revno:" + revision else: destination.write(line) ``` Thanks for the response so far, my question now is rather: is this the way to do it? if a line does not have any (of the known)datasets in it, then i want to print it if it is a dataset?