Inserting into a sublist based on length

I’ve hit a brick wall with this attempted code implementation. I am basically trying to search through a large list of sublists and find any sublist greater than 10 in length. I then want to split the sublist in half and add the first element of the list to the sublist that has been split.

lst0 = [['H2:blah', '1', '3', '4','5'], ['H2:blahhh', '12','13','14','15','16','17','18','19','20'],['H2:blaher', '22','23','24','25','26'], ['H2:blahhh', '99','98','97','97','12','13','14','15','16','17','18','19','20']]
for z in lst0:
    
    if len(z) > 10:
     print((z))

['H2:blahhh', '99', '98', '97', '97', '12', '13', '14', '15', '16', '17', '18', '19', '20']

I would then need to split the sub list like this…

['H2:blahhh', '99', '98', '97', '97', '12', '13']
['H2:blahhh', '14', '15', '16', '17', '18', '19', '20']

and then put it all back together in the main list…

lst0 = [['H2:blah', '1', '3', '4','5'], ['H2:blahhh', '12','13','14','15','16','17','18','19','20'],['H2:blaher', '22','23','24','25','26'], ['H2:blahhh', '99', '98', '97', '97', '12', '13'],
['H2:blahhh', '14', '15', '16', '17', '18', '19', '20'

]

Any pointers would be great. I try to find the sublist number but it’s not so clear how do this.

It looks like the first element of each sublist is different from all the others, so I would be inclined to first split that off. Then you can fracture the list in half, and build two new lists out of the halves:

label, *data = z
half = len(data) // 2
first = data[:half]
second = data[half:]
lst0.append([label, *first])
lst0.append([label, *second])

That takes care of the splitting. How you end up putting it back depends on your requirements, though. For example, what happens if you have 80 elements? After splitting into a pair of 40s, should it see them again and split into 20s, or leave them as 40s? One good option, if you don’t want to re-split, would be to build a new list of all the results; anything small enough to not be split gets appended as-is, and the others get split and then appended as two separate pieces. But if you want to divide more than once, it may be worth doing an indexed replacement of some sort. (It might also be worth doing all the replacements at once, though.)

This is what I had in mind …

import re
if len(z) > 10:
     print((z))
     splst = round(len(z)/2)
     print(splst)
     for zz in z:
         H2 = re.search('(?i)H2:',zz)
         if H2:
                add_new_H2 = zz
                print (add_new_H2)
                
     #print (z)
     #for cheese in z:
     z.insert(splst, add_new_H2)
         
print (z)

['H2:blahhh', '99', '98', '97', '97', '12', '13', 'H2:blahhh', '14', '15', '16', '17', '18', '19', '20']

So I’ve inserted the a duplicate H2 in the middle of the sublist. Then I split the the sublist with the ‘half’ method. But I have no idea how to put the two new sublists into the main list and replace the lenghty sublist I’ve just split.

I can’t see a way to replace by sublist index. I would need to get the index of the sublist i want to replace and then del and insert.

This gives me the index :slight_smile:
lst0.index(z))

I’d build a new result list instead of modifying the original in-place:

lst0 = [['H2:blah', '1', '3', '4', '5'], ['H2:blahhh', '12', '13', '14', '15', '16', '17', '18', '19', '20'],['H2:blaher', '22', '23', '24', '25', '26'], ['H2:blahhh', '99', '98', '97', '97', '12', '13', '14', '15', '16', '17', '18', '19', '20']]
new_list = []

for z in lst0:
    if len(z) > 10:
        half = len(z) // 2
        new_list.append(z[ : half])
        new_list.append(z[ : 1] + z[half : ])
    else:
        new_list.append(z)

Superb , that does it thanks