Python: Split a list into sub-lists based on index ranges

后端 未结 5 2081
感情败类
感情败类 2020-12-10 10:43

UPDATED:

In python, 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]
5条回答
  •  春和景丽
    2020-12-10 11:23

    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

提交回复
热议问题