How do I split a list into sub-lists based on index ranges?
e.g. original list:
list1 = [x,y,z,a,b,c,d,e,f,g]
using index ranges 0–4:
list1a = [x,y,z,a,b]
using index ranges 5–9:
list1b = [c,d,e,f,g]
I already known the (variable) indices of list elements which contain certain string and want to split the list based on these index values.
Also need to split into variable number of sub-lists, i.e.:
list1a
list1b
.
.
list1[x]
Note that you can use a variable in a slice:
l = ['a',' b',' c',' d',' e']
c_index = l.index("c")
l2 = l[:c_index]
This would put the first two entries of l in l2