Hi!

I need to split a list if item A is followed by item B.
I'm really stuck at this and the only thing i could think of is that i should use regex, but i dont know how.

Example of list:
list =

I would like the output to be:
REF123 REF123
REF123
REF123


/Pluring

Dani AI

Generated

The sample shows a flat list of emails and REF tokens being grouped so that a new group starts whenever a non-email token is immediately followed by an email token (the first output line ends at the second REF because the next item is an email). solved it with index(); correctly warned about index() returning the first match and posted a working single‑pass print loop; pointed out that join() is useful for formatting the final lines.

A cleaner, safer approach is a single pass that builds sublists (or yields groups) using an index-based lookahead and a proper email test (a simple regex rather than just checking for '@'). This avoids the index() pitfall with duplicate values and is explicit about the splitting rule:

import re

email_re = re.compile(r'^[^@\s]+@[^@\s]+\.[^@\s]+$')

def split_groups(items):
    groups = []
    cur = []
    for i, token in enumerate(items):
        cur.append(token)
        if i + 1 < len(items):
            next_tok = items[i + 1]
            if (not email_re.match(token)) and email_re.match(next_tok):
                groups.append(cur)
                cur = []
    if cur:
        groups.append(cur)
    return groups

For situations where REF tokens follow a strict naming pattern (for example REF\d+), an alternative is to join the list into a string and use a regex split with lookbehind/lookahead to cut at the space between REF and the following email. That can be compact but is more fragile if tokens contain spaces or the REF pattern changes.

Notes: prefer enumerate over list.index() for position tracking; prefer a regex for email detection instead of a bare '@' test; format final output by joining tokens of each subgroup (avoids Python2 print quirks). Test edge cases like consecutive REFs and trailing tokens.

Solved it with index().

pos = list.index(i)
if "@" in list[pos + 1]:

Usually you split() a string into a list.

You can join() a list into a string like this:

lst = ['email@email.com','REF']
separator = ' ' # this one space will be the string between each list element
string = separator.join( lst )

I have been playing with it and didn't get index() to work, mainly because index always returns the first instance of the string if it occurs multiple times. I assume that normally it would be different values and then it wouldn't matter. But just in case the email is repeated somewhere in your lists, the only way I could get it to work was like this:

for a in range(len(list)):
     if '@' in list[a]: print list[a],
     else:
         if a < (len(list)-1):
             if '@' in list[a + 1]:
                 print list[a]
             else:
                 print list[a],
         else:
             print list[a]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.