Python Forum
split string and keep separator - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: split string and keep separator (/thread-14246.html)



split string and keep separator - wardancer84 - Nov-21-2018

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



RE: split string and keep seperator - Gribouillis - Nov-21-2018

You can use result = [s[:1+s.find('=')] or s for s in options]


RE: split string and keep separator - wardancer84 - Nov-21-2018

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=']