1

Splitting list elements based on substring

How do I split the elements in this element based on the string before the dot without explicitly writing it in code?

lst = ['ds_a.cola','ds_a.colb','ds_b.cola','ds_b.colb']

Since there are two variants of 'ds'. I want two lists.

lst_dsa = ['ds_a.cola','ds_a.colb']
lst_dsb = ['ds_b.cola','ds_b.colb']

My old code was:

lst_dsa = []
lst_dsb = []
for item in lst :
    if "ds_a" in item:
        lst_dsa.append(item)
    else:
        lst_dsb.append(item)

But I can't use this since there might be more than 2, like, ds_c,ds_d.... How do I achieve this in python?

Submitted October 11th 2021 by Admin

Answers
0

Try itertools.groupby:

>>> from itertools import groupby
>>> [list(v) for _, v in groupby(lst, key=lambda x: x[x.find('_') + 1])]
[['ds_a.cola', 'ds_a.colb'], ['ds_b.cola', 'ds_b.colb']]
>>> 

Admin | 1 week ago


0

Use a dictionary:

from collections import defaultdict lst = ['ds_a.cola','ds_a.colb','ds_b.cola','ds_b.colb'] d = defaultdict(list)
for e in lst: key, _ = e.split(".") d[key].append(e) print(d)

Output

defaultdict(<class 'list'>, {'ds_a': ['ds_a.cola', 'ds_a.colb'], 'ds_b': ['ds_b.cola', 'ds_b.colb']})

Admin | 1 week ago


0

You can map them:

from collections import defaultdict lst = ['ds_a.cola','ds_a.colb','ds_b.cola','ds_b.colb']
ds_dict = defaultdict(list) for item in lst: key, value = item.split(".") ds_dict[key].append(value) print(dict(ds_dict))

Output:

{'ds_a': ['cola', 'colb'], 'ds_b': ['cola', 'colb']}

Admin | 1 week ago


0

Use a dict and hold the data

from collections import defaultdict
lst = ['ds_a.cola','ds_a.colb','ds_b.cola','ds_b.colb','ds_x.cola','ds_x.colb']
data = defaultdict(list)
for entry in lst: a,_ = entry.split('.') data[a].append(entry)
print(data)

output

defaultdict(<class 'list'>, {'ds_a': ['ds_a.cola', 'ds_a.colb'], 'ds_b': ['ds_b.cola', 'ds_b.colb'], 'ds_x': ['ds_x.cola', 'ds_x.colb']})

Admin | 1 week ago



Relevant Questions