Combining elements of a list if condition is met
<p>I have some python code, with its final output being a list:</p>

<pre><code>['JAVS P 0 060107084657.30', '59.41 0 S', ' CEY P 0 060107084659.39', '03.10 0 S', 'CADS P 0 060107084703.67', 'VISS P 0 060107084704.14']
</code></pre>

<p>Now, I would like to join the lines, that do not start with sta (JAVS, CEY, CADS, VISS,...) with the previous one.
I get this element of the list with:</p>

<pre><code>if not element.startswith(sta):
  print element
</code></pre>

<p>How to proceed with joining the elements?</p>

<p>Final output should be like this:</p>

<pre><code>[u'JAVS P 0 060107084657.30       59.41 0 S']
[u' CEY P 0 060107084659.39       03.10 0 S']
[u'CADS P 0 060107084703.67']
[u'VISS P 0 060107084704.14']
</code></pre>

<p>Full code:</p>

<pre><code>import glob
from obspy import read_events
import itertools

####### Definitions

def pairwise(iterable):
    "s -&gt; (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = itertools.tee(iterable)
    next(b, None)
    return itertools.izip(a, b)

####### Script

#write_file = open("hypoell", "w")

path = 'events/'
event_files = glob.glob(path + '*.hyp')

fmt_p = '%s %s %s %s'
fmt_s = '%s %s %s'


for event_file in event_files:

  cat = read_events(event_file)
  event = cat[0]

  lines = []

  for pick in event.picks:
    sta = pick.waveform_id.station_code
    if len(sta) &lt;= 3:
        sta = " "+sta
    else:
        sta=sta
    phase = pick.phase_hint

    year = pick.time.year
    month = pick.time.month
    day = pick.time.day
    hh = pick.time.hour
    minute = pick.time.minute
    sec = pick.time.second
    ms = pick.time.microsecond


    p_time = pick.time.strftime("%Y%m%d%H%M%S.%f")
    p_time = p_time[2:-4]

    s_time = pick.time.strftime("%Y%m%d%H%M%S.%f")
    s_time = s_time[12:-4]

    if phase == "P":
        p_line = fmt_p %(sta, phase, 0, p_time)
        lines.append(str(p_line))
    if phase == "S":
        s_line = fmt_s %(s_time, 0, "S")
        lines.append(str(s_line))

########################################################################
print lines

prefixes = ('JAVS', ' CEY', 'CADS', 'VISS')

for a, b in pairwise(lines):
    if a[0].startswith(prefixes):
        if not b[0].startswith(prefixes):
            print a + b
        else:
            print a
</code></pre>

<p>Here is a simple loop assuming <code>mydata</code> is non-empty.</p>

<pre><code>#!/usr/bin/env python

mydata = [
    [u'JAVS P 0 060107084657.30'],
    ['59.41 0 S'],
    [u' CEY P 0 060107084659.39'],
    ['03.10 0 S'],
    [u'CADS P 0 060107084703.67'],
    [u'VISS P 0 060107084704.14'],
]

prefixes = (u'JAVS', u' CEY', u'CADS', u'VISS')

a = [u'']
for b in mydata:
    if a[0].startswith(prefixes):
        if not b[0].startswith(prefixes):
            print([a[0] + "  " + b[0]])
        else:
            print([a[0]])
    a = b
print([a[0]])
</code></pre>

<p>with output </p>

<pre><code>['JAVS P 0 060107084657.30  59.41 0 S']
[' CEY P 0 060107084659.39  03.10 0 S']
['CADS P 0 060107084703.67']
['VISS P 0 060107084704.14']
</code></pre>

python