title: Most elegant way to separate list based on pattern (Python)
body: <p>I have a pandas column of lists of consecutive log actions that users do, while posting a photo in mobile app in each whole logging session. Suppose the single list looks like:</p>
  
  <pre><code>my_list = [
        'action_a', 'action_b', 'action_c', 'action_z', 
        'action_j',
        'action_a','action_b', 
        'action_a', 'action_b', 'action_z']
  </code></pre>
  
  <p>1) action_a - the start of photo upload</p>
  
  <p>2) action_z - the end of photo upload</p>
  
  <p>3) other actions_i - all the actions that can occur between action_a and action_z. </p>
  
  <p>4) there may be errors, like 'action_j', that appear not between 'action_a', 'action_z' and we shouldn't take them into account</p>
  
  <p>5) the process of photo upload may not be completed - so there may be path like 'action_a','action_b'.</p>
  
  <p>The GOAL = separate my_list into sublists of all action paths that starts with 'action_a' and ends with 'action_z' or ends before another 'action_a'. So the result should be like that:</p>
  
  <pre><code>['action_a', 'action_b', 'action_c', 'action_z'] 
  ['action_a','action_b']
  ['action_a', 'action_b', 'action_z']
  </code></pre>
  
  <p>So currently I am trying to solve it like that: firstly I've deleted all my_lists, where the number of 'action_z' is greater than the number of 'action_a' or where there is no 'action_a'. Then I did that:</p>
  
  <pre><code>indices_a = [i for i, x in enumerate(my_list) if x == "action_a"]
  indices_z = [i for i, x in enumerate(my_list) if x == "action_z"]
  
  if(len(indices_z)&lt;1):
      for i_a,x_a in enumerate(indices_a):
          if (i_a+1 != len(indices_a)):
              indices_z.append(indices_a[i_a+1]-1) 
          else: indices_z.append(len(my_list)-1) 
  else:       
      for i_a,x_a in enumerate(indices_a):
          if (i_a+1 != len(indices_a)):
              if (indices_z[i_a] &gt; indices_a[i_a+1] ):
                  indices_z.insert(i_a, indices_a[i_a+1]-1)
          else:  indices_z.append(len(my_list)-1) 
  
  res=[]
  for i,j in zip(indices_a, indices_z):
      res.append(my_list[i:j+1] )
  </code></pre>
  
  <p>Seems like it works. What is the better way?</p>