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]
mkrieger1
15.7k4 gold badges45 silver badges57 bronze badges
asked Sep 2, 2013 at 10:15
1
In python, it's called slicing. Here is an example of python's slice notation:
>>> list1 = ['a','b','c','d','e','f','g','h', 'i', 'j', 'k', 'l']
>>> print list1[:5]
['a', 'b', 'c', 'd', 'e']
>>> print list1[-7:]
['f', 'g', 'h', 'i', 'j', 'k', 'l']
Note how you can slice either positively or negatively. When you use a negative number, it means we slice from right to left.
answered Sep 2, 2013 at 10:20
TerryATerryA
56.9k11 gold badges117 silver badges137 bronze badges
1
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
answered Sep 2, 2013 at 10:33
0
If you already know the indices:
list1 = ['x','y','z','a','b','c','d','e','f','g']
indices = [[0, 4], [5, 9]]
print [list1[s:e+1] for s,e in indices]
Note that we're adding +1 to the end to make the range inclusive...
answered Sep 2, 2013 at 10:31
Jon Clements♦Jon Clements
134k31 gold badges240 silver badges273 bronze badges
list1a=list[:5]
list1b=list[5:]
answered Sep 2, 2013 at 10:17
no1no1
7172 gold badges8 silver badges21 bronze badges
5
One of the ways to do it if you have multiple indexes or know the range of indexes you need to get:
split_points - points where you will split your string or list
k - the range you need to split, example = 3
split_points = [i for i in range[0, len[string], k]]
parts = [string[ind:ind + k] for ind in split_points]
answered Apr 11, 2021 at 18:32
list1=['x','y','z','a','b','c','d','e','f','g']
find=raw_input["Enter string to be found"]
l=list1.index[find]
list1a=[:l]
list1b=[l:]
answered Sep 2, 2013 at 10:28
no1no1
7172 gold badges8 silver badges21 bronze badges
0
Consider the core pesudocode of the following example:
def slice_it[list_2be_sliced, indices]:
"""Slices a list at specific indices into constituent lists.
"""
indices.append[len[list_2be_sliced]]
return [list_2be_sliced[indices[i]:indices[i+1]] for i in range[len[indices]-1]]
answered Mar 4, 2021 at 21:17
ShadyShady
1661 silver badge6 bronze badges
This is the way I do it, if the input is a list of indices on which to split an array:
#input
list1 = ['x','y','z','a','b','c','d','e','f','g']
split_points = [2,5,8]
#split array on indices:
s = split_points+[len[list1]] #must contain index beyond last element, alternatively use directly split_points.append[len[list1]]
print[[list1[i1:i2] for i1,i2 in zip[[0]+s[:-1],s]]]
>>> [['x', 'y'], ['z', 'a', 'b'], ['c', 'd', 'e'], ['f', 'g']]
answered Jun 27 at 16:37
VincenzoooVincenzooo
1,7831 gold badge15 silver badges29 bronze badges