title: How to split a list on an element delimiter
body: <p>Are there concise and elegant ways of splitting a list in Python into a list of sub-lists by a delimiting element, such that <code>['a', 'delim', 'b']</code> -> <code>[['a'], ['b']]</code>?</p>
  
  <p>Here is the example:</p>
  
  <pre><code>ldat = ['a','b','c','a','b','c','a','b']
  dlim = 'c'
  lspl = []   # an elegant python one-liner wanted on this line!
  print(lspl) # want: [['a', 'b'], ['a', 'b'], ['a', 'b']]
  </code></pre>
  
  <h2>Working examples that seem overly complex</h2>
  
  <p>I have surveyed documentation and related questions on stackoverflow - many referenced below - which did not answer my question, and am summarizing my research below: several approaches which <em>do</em> generate the desired output, but are verbose and intricate, and what is happening (splitting a list) is not immediately apparent -- you really have to squint.</p>
  
  <p>Are there better ways? I am primarily interested in readability for beginners (e.g. teaching), canonical / 'Pythonic' approaches, and secondarily in the most efficient approaches (e.g. timeit speed). Ideally answers would address both Python 2.7 and 3.x.</p>
  
  <h3>with conditional .append()</h3>
  
  <p>Loop through the list and either append to the last output list or add a new output list. Based on an example that <a href="https://stackoverflow.com/a/27003351/7207622">includes the delimiter</a>, but altered to exclude it. I'm not sure how to make it a one-liner, or if that is even desirable.</p>
  
  <pre><code>lspl = [[]]
  for i in ldat:
      if i==dlim:
          lspl.append([])
      else:
          lspl[-1].append(i)
  print(lspl) # prints: [['a', 'b'], ['a', 'b'], ['a', 'b']]
  </code></pre>
  
  <h3>with itertools.groupby</h3>
  
  <p>Combine itertools.groupby with list comprehension. Many answers <a href="https://stackoverflow.com/a/27003860/7207622">include  delimeters</a>, this is based on those that <a href="https://stackoverflow.com/a/15358422/7207622">exclude delimeters</a>.</p>
  
  <pre><code>import itertools
  lspl = [list(y) for x, y in itertools.groupby(ldat, lambda z: z == dlim) if not x]
  print(lspl) # prints: [['a', 'b'], ['a', 'b'], ['a', 'b']]
  </code></pre>
  
  <h3>with slicing on indices</h3>
  
  <p>Some related questions have discussed how to use slicing after using .index() -- however answers usually focus on finding the <a href="https://stackoverflow.com/a/18571084/7207622">first index only</a>. One can extend this approach by first <a href="https://stackoverflow.com/a/6294205/7207622">finding a list of indices</a> and then looping through a self-zipped list to slice the ranges. </p>
  
  <pre><code>indices = [i for i, x in enumerate(ldat) if x == dlim]
  lspl = [ldat[s+1:e] for s, e in zip([-1] + indices, indices + [len(ldat)])]
  print(lspl) # prints: [['a', 'b'], ['a', 'b'], ['a', 'b']]
  </code></pre>
  
  <p>However, like all the approaches I have found, this seems like a very complex way of enacting a simple split-on-delimiter operation.</p>
  
  <h2>Comparison to string splitting</h2>
  
  <p>By comparison and as a model only, here is a working, concise, and elegant way of splitting
  a string into a list of sub-strings by a delimiter.</p>
  
  <pre><code>sdat = 'abcabcab'
  dlim = 'c'
  sspl = sdat.split(dlim)
  print(sspl) # prints: ['ab', 'ab', 'ab']
  </code></pre>
  
  <p><em>NOTE: I understand there is no <code>split</code> method on lists in Python, and I am not asking about splitting a string. I am also not asking about splitting element-strings into new elements.</em></p>