Python Forum

Full Version: split string and keep separator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
often discussed on the interwebs, but no i idiot-proof solution available.

i want to split the following list of strings by the "=" and keep the left part incl. the "=", and the re module should NOT be used for this.

>>> print(options)
['SYSTEM=LDAP', 'registry=LDAP']
output should look like this -> ['SYSTEM=', 'registry=']

best would be to integrate this in this generator

def arguments_generator(options):
        for element in options:
                yield '-a'
                yield element
You can use result = [s[:1+s.find('=')] or s for s in options]
wow, thank you, works perfectly!

>>> mylist = ['SYSTEM=LDAP', 'registry=LDAP']
>>> result = [s[:1+s.find('=')] or s for s in mylist]
>>> print(result)
['SYSTEM=', 'registry=']