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

Discussão em 'Python' iniciado por Stack, Fevereiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    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]

    Continue reading...

Compartilhe esta Página