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:
email@email.com email@email.com email@email.com REF123 REF123
email@email.com REF123
email@email.com REF123


/Pluring

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.