title: Avoiding repeat of code after loop?
body: <p>I often end up writing a bit of code twice when using a loops. For example, while going over the Udacity computer science course, I wrote the code (for a function to find the most sequentially repeated element):</p>
  
  <pre><code>def longest_repetition(l):
      if not l:
          return None
      most_reps = count = 0 
      longest = prv = None
      for i in l:
          if i == prv:
              count += 1
          else:
              if count &gt; most_reps:
                  longest = prv
                  most_reps = count
              count = 1
          prv = i
      if count &gt; most_reps:
          longest = prv
      return longest
  </code></pre>
  
  <p>In this case, I'm checking twice if the count is greater than the previously most repeated element. This happens both when the current element is different from the last and when I've reached the end of the list.</p>
  
  <p>I've also run into this a few times when parsing a string character by character. There have also been a few times where it's been up to about 5 lines of code. Is this common, or a result of the way I think/code. What should I do?</p>
  
  <p>edit: Similarly, in a contrived string splitting example:</p>
  
  <pre><code>def split_by(string, delimeter):
      rtn = []
      tmp = ''
      for i in string:
          if i == delimeter:
              if tmp != '':
                  rtn.append(tmp)
                  tmp = ''
          else:
              tmp += i
      if tmp != '':
          rtn.append(tmp)
      return rtn
  </code></pre>
  
  <p>edit: The exam this was from was written for students of the course who are not expected to have any outside knowledge of Python; only what was taught in the previous units. Although I do have prior experience in Python, I'm trying to adhere to these restrictions to get the most of the course. Things like str.split, lists, and a lot of the fundamentals of Python were taught, but nothing yet on imports - especially not things like groupby. That being said, how should it be written without any of the language features that probably wouldn't be taught in a programming introduction course.</p>