removing list from nested list if condition true
You can group your data by their transport_mode and for consecutive stretches of 'B' modify them:
from itertools import groupby
# testcase 2
data = [['3786', 'AEJEA', 'B'], ['AEJEA', 'GBLGP', 'B'], ['GBLGP', 'USMSY', 'B'],
['USMSY', 'DNTS', 'K'], ['DNTS', 'NEWORL_LA', 'K'], ['NEWORL_LA', 'LBEACH_CA', 'K'],
['LBEACH_CA', 'USLGB', 'K'], ['USLGB', 'CNSHG', 'B'], ['CNSHG', 'EJ00', 'K'],
['EJ00', '354B', 'B']]
result = []
# group by the last element ("transport_mode") of inner elements
# and combine consecutive "B"oat stops into one
for grp,val in groupby (data, lambda x:x[-1]):
# if B create one merged elements
if grp == "B":
vals = list(val)
# use the first B's 1st value and
# 2nd to last value of last B
r = [vals[0][0], *vals[-1][1:]]
result.append(r)
# take all elements
else:
result.extend(val)
print (result)
# print your current result for testcase 2
print( [['3786', 'USMSY', 'B'], ['USMSY', 'DNTS', 'K'], ['DNTS', 'NEWORL_LA', 'K'],
['NEWORL_LA', 'LBEACH_CA', 'K'], ['LBEACH_CA', 'USLGB', 'K'],
['USLGB', 'CNSHG', 'B'], ['CNSHG', 'EJ00', 'K'], ['EJ00', '354B', 'B']])
Output:
[['3786', 'USMSY', 'B'], ['USMSY', 'DNTS', 'K'], ['DNTS', 'NEWORL_LA', 'K'], ['NEWORL_LA', 'LBEACH_CA', 'K'], ['LBEACH_CA', 'USLGB', 'K'], ['USLGB', 'CNSHG', 'B'], ['CNSHG', 'EJ00', 'K'], ['EJ00', '354B', 'B']]
[['3786', 'USMSY', 'B'], ['USMSY', 'DNTS', 'K'], ['DNTS', 'NEWORL_LA', 'K'], ['NEWORL_LA', 'LBEACH_CA', 'K'], ['LBEACH_CA', 'USLGB', 'K'], ['USLGB', 'CNSHG', 'B'], ['CNSHG', 'EJ00', 'K'], ['EJ00', '354B', 'B']]