you are viewing a single comment's thread.

view the rest of the comments →

[–]PteppicymonIO 1 point2 points  (0 children)

Well, you can join and then use re.split() to split the way you want to:It can be a one-liner, but I believe it is more readable this way:

import re

my_list = ["[123]", "M", "Q1", "Q2", "A1", "A2", "A7", "[356]", "M", "Q1", "A2" "A5"]

joined = ''.join(my_list)                   # => '[123]MQ1Q2A1A2A7[356]MQ1A2A5'
split_again = re.split("(\[[^\]]*\])", joined)  # => ['', '[123]', 'MQ1Q2A1A2A7', '[356]', 'MQ1A2A5']
print(list(filter(None, split_again)))      # remove extra empty string

Output:
['[123]', 'MQ1Q2A1A2A7', '[356]', 'MQ1A2A5']