title: split string into groups of given size
body: <p>My situation is I have a list of time-stamped items, but the actual time-of-day is all squished together. Like this:</p>
  
  <pre><code>time = "165420" # 16:54:20 (or, 4:54 PM and 20 seconds.)
  </code></pre>
  
  <p>I wanted a script to go over the lines and be able to convert the time-stamp into a date-time. I have the year, month, day already that was no problem. But for the time situation I was desperate to find an elegant and simple solution. I decided to brute force it at first to continue with my code, and ended up with:</p>
  
  <pre><code>import datetime
  
  dt_timestamp = extract_dt("2012-11-09-165420") # example
  
  def extract_dt(timestamp):
      ds = timestamp.split('-') #split into groups (0,1,2 are the date, 3 is the time.
      d = [int(i) for i in ds[:3]] + [int(ds[3][0:2]), int(ds[3][2:4]), int(ds[3][4:6])]
      return datetime.datetime(*d)
  </code></pre>
  
  <p>While trying to solve my situation I've seen many variations on this question, with many answers.</p>
  
  <p>I have found <a href="http://bytes.com/topic/python/answers/157385-splitting-string-into-groups-three-characters" rel="nofollow">this solution</a>:</p>
  
  <pre><code>def nsplit(s, n):
      return [s[k:k+n] for k in xrange(0, len(s), n)]
  </code></pre>
  
  <p>Which works nicely for me:</p>
  
  <pre><code>&gt;&gt;&gt; s = "165420"
  &gt;&gt;&gt; n = 2
  &gt;&gt;&gt; [s[k:k+n] for k in xrange(0, len(s), n)]
  ['16', '54', '20']
  </code></pre>
  
  <p>But I am pretty sure there MUST be a better, more elegant way of splitting a string into groups of equal size, as was mentioned in the <a href="http://bytes.com/topic/python/answers/157385-splitting-string-into-groups-three-characters" rel="nofollow">link</a> I provided (but fixed because the link poster had a typo):</p>
  
  <blockquote>
    <p>TheFunction("Hello World",3)</p>
    
    <p>Returns:</p>
    
    <p>['Hel','lo ','Wor','ld']</p>
  </blockquote>
  
  <p>I would prefer, if possible, that it not be a function, I want a way to do it in-line. And it should be as efficient as possible. I can always extract the list-comprehension from the function I found, but I have a feeling its not very efficient, and it just doesn't look right.</p>
  
  <p>The answer is probably something very simple I just didn't know about. Thanks again.</p>