List Comprehension gurus

Ok gurus here is something I think can be shrunk down.
We have an XML file that we take and using xmltodict turn it into a dataset.
We end up with 50 rows of data and 7 columns.
Column 5 in the dataset contains a string that we parse using .split(’_’) that splits into 13 values.
What I need to do is loop the DS, parse col 5, take the 13 values from col 5, and then combine those values to the end of that record.
Write the DS to a Dataset tag.

Basically take xDS turn it into a pyDataSet, loop it, parsing col 5, take 13 values from col5, put them on the end of the record.

xDS=['Col1','Col2','Col3','Col4','a_b_c_d_e_f_g_h_i_j_k_l_m','Col6','Col7']
ResultDS=['Col1','Col2','Col3','Col4','a_b_c_d_e_f_g_h_i_j_k_l_m','Col6','Col7','Col8','Col9','Col10','Col11','Col12','Col13','Col14','Col15','Col16','Col17','Col18','Col19','Col20']

Impress me list comprehension gurus.
I can do it, but the old man way lol

newRows = [[x for x in row]+row[4].split('_') for row in system.dataset.toPyDataset(xDS)]
3 Likes

If the split won’t always yield thirteen values, you can use this:

newRows = [[x for x in row]+(row[4].split('_')+[None]*13)[:13] for row in system.dataset.toPyDataset(xDS)]
5 Likes

@pturmel, do you ever sleep?

2 Likes

I travel a lot. Ya never know what time zone I’ll be in. /:

1 Like