Divide a list into n sublists

Asked

Viewed 14,487 times

7

I have the following list:

l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

What I need is to divide this list into n sublists, in this case it is not possible to do it manually because the n will be dynamic.

3 answers

8


l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

You can do so if you want 3 sublists:

n = 3
splited = [l[i::n] for i in range(n)]
print(splited) # [[0, 3, 6, 9, 12], [1, 4, 7, 10, 13], [2, 5, 8, 11, 14]]

If you want sublist elements to maintain sequence:

n = 3
splited = []
len_l = len(l)
for i in range(n):
    start = int(i*len_l/n)
    end = int((i+1)*len_l/n)
    splited.append(l[start:end])
print(splited) # [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]

Note that in this case the number of elements is divisible by n (3), if we had for example n = 4 the number of elements in each sub-list would be different:

splited = [[0, 4, 8, 12], [1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11]]

Or, following the second example:

splited = [[0, 1, 2], [3, 4, 5, 6], [7, 8, 9, 10], [11, 12, 13, 14]]
  • Excellent answer. It’s only nice to avoid the letter l as variable name - even short-lived, on account of readability,

  • Obgado @jsbueno, just put l because it’s list name also in question

5

You can use the range:

def chunks(lista, n):
    for i in range(0, len(lista), n):
        yield lista[i:i + n]

l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
print(list(chunks(l, 3)))

# [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14]]

Source

Is made a iteration on the list, by specifying the third argument of range(), step, is indicated at which step the iteration, and by using the yield the pieces will be returned maintaining the state where it stopped, until the end of the iteration.

See demonstração

Note: The above function will generate sublists of n elements in the example, 3.

To split the list into n sublists, do so:

def chunks(lista, n):
    inicio = 0
    for i in range(n):
        final = inicio + len(lista[i::n])
        yield lista[inicio:final]
        inicio = final

l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
print(list(chunks(l, 3)))

# [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]

Source

To split the list into n sublists is informed the beginning and final, the beginning is where you want to start sharing, final is the amount of elements that each sublist will have, the yield returns to sublist, and then the variable is assigned inicio the value of final, representing the position of sublist previous, so successively until the end of the iteration.

See demonstração


In the questions below there are some explanations about the yield:

  1. What is the use of the reserved word yield?
  2. What is the purpose of the yield?

0

Here:

aLista = [1, 2, 3 , 4, 5, 6, 7, 8, 9]

aLista = iter(aLista)
result = []

for i in aLista:
    try:
        result.append([i, next(aLista)])
    except Exception:
        result.append([i])

print(result)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.