Search code examples
pythonpysparksplitpython-re

Python split() without removing the delimiter in a list


We have this list:

lista = ['int32',
 'decimal(14)',
 'int(32)',
 'string',
 'date',
 'decimal(27,2)',
 'decimal(17,2)']

And we need:

['int32', 'decimal', '(14)',
 'int', '(32)',
 'string',
 'date',
 'decimal','(27,2)',
 'decimal','(17,2)']

We use

for i in lista:
    .split('(')

but we lose ( in the process.


Solution

  • works this code :)

    lista = ['int32','decimal(14)','int(32)','string','date','decimal(27,2)','decimal(17,2)']
    lst = []
    for i in lista:
        x= (i.find('('))
        if(x != -1):
            lst.append(i[0:x])
            lst.append(i[x::])
        else:
            lst.append(i)
    print(lst)