Split list python by index

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.:


Split list python by index


15.7k4 gold badges45 silver badges57 bronze badges

asked Sep 2, 2013 at 10:15

Split list python by index


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

Split list python by index


56.9k11 gold badges117 silver badges137 bronze badges


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

Split list python by index


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

Split list python by index

Jon ClementsJon Clements

134k31 gold badges240 silver badges273 bronze badges


answered Sep 2, 2013 at 10:17


7172 gold badges8 silver badges21 bronze badges


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

find=raw_input("Enter string to be found")

answered Sep 2, 2013 at 10:28


7172 gold badges8 silver badges21 bronze badges


Consider the core pesudocode of the following example:

def slice_it(list_2be_sliced, indices):
    """Slices a list at specific indices into constituent lists.
    return [list_2be_sliced[indices[i]:indices[i+1]] for i in range(len(indices)-1)]

answered Mar 4, 2021 at 21:17


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:

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

Split list python by index


1,7831 gold badge15 silver badges29 bronze badges

How do you split a specific index in Python?

Indexes in Python are zero-based, so the first item in a list has an index of 0 , the second an index of 1 , etc..
Use the str. split() method to split the string into a list..
Access the list element at the specific index..
Assign the result to a variable..

How do you split a list into two parts in Python?

This can be done using the following steps:.
Get the length of a list using len() function..
If the length of the parts is not given, then divide the length of list by 2 using floor operator to get the middle index of the list..
Slice the list into two halves using [:middle_index] and [middle_index:].

How do you split a list in a list Python?

To split the elements of a list in Python: Use a list comprehension to iterate over the list. On each iteration, call the split() method to split each string. Return the part of each string you want to keep.

How do you split a list into delimiter in Python?

The split() method of the string class is fairly straightforward. It splits the string, given a delimiter, and returns a list consisting of the elements split out from the string. By default, the delimiter is set to a whitespace - so if you omit the delimiter argument, your string will be split on each whitespace.

Tải thêm tài liệu liên quan đến bài viết Split list python by index