I know there are very similar questions to this, but my specific case is a bit unique. Let's say I have a list, lst
, of data:
['1234 56 789',
'12345',
'x',
'y',
'9876 54 321',
'54321',
'x',
'1234 98 765',
'12398',
'x',
'y']
What I am trying to do is make sublists within this list. My goal is to start a new sublist at every unique identifier (which in this list are the long strings with two spaces). Initially, I realized I could run the following code:
[lst[i:i+4] for i in range(0, len(lst),4)]
But I noticed that not every unique identifier has a y
value, so the sublists aren't created correctly, as shown below:
[['1234 56 789', '12345', 'x', 'y'],
['9876 54 321', '54321', 'x', '1234 98 765'],
['12398', 'x', 'y']]
My desired output is the following. A list of sublists that start at every unique identifier
[['1234 56 789', '12345', 'x', 'y'],
['9876 54 321', '54321', 'x'],
['1234 98 765','12398', 'x', 'y']]
I realized I could potentially run some loop that checks if a given item is a unique identifier, and if it isn't, put it in a sublist, but if it is, start a new sublist.
I have attempted the following:
lists = [[]]
for i in range(0, len(lst)):
if (lst[i][0].isdigit()) and (len(lst[i]) > 10): # If i is a unique identifier
lists.append([lst[i]]) # Start new sub-list
else:
lists[len(lists)-1].append(lst[i]) # Add it to the last sub-list
print(lists)
But I get an error:
IndexError: string index out of range
I feel like I am super close, but I have spent enough time on this to the point where I wanted to ask for help. I have showed my thought process and code. Any help is appreciated. Thanks.