Skip to content Skip to sidebar Skip to footer

Creating Sublists

The opposite of list flattening. Given a list and a length n return a list of sub lists of length n. def sublist(lst, n): sub=[] ; result=[] for i in lst: sub+=[i]

Solution 1:

Such a list of lists could be constructed using a list comprehension:

In [17]: seq=[1,2,3,4,5,6,7,8]
In [18]: [seq[i:i+3] for i in range(0,len(seq),3)]
Out[18]: [[1, 2, 3], [4, 5, 6], [7, 8]]

There is also the grouper idiom:

In [19]: import itertools
In [20]: list(itertools.izip_longest(*[iter(seq)]*3))
Out[20]: [(1, 2, 3), (4, 5, 6), (7, 8, None)]

but note that missing elements are filled with the value None. izip_longest can take a fillvalue parameter as well if something other than None is desired.


list1+=[list2] -- noting the brackets this time -- is equivalent to list1.append(list2). My highest priority when writing code is readability, not speed. For this reason, I would go with list1.append(list2). Readability is subjective, however, and probably is influenced greatly by what idioms you're familiar with.

Happily, in this case, readability and speed seem to coincide:

In [41]: %timeit list1=[1,2,3]; list1.append(list2)
1000000 loops, best of 3: 612 ns per loop

In [42]: %timeit list1=[1,2,3]; list1+=[list2]
1000000 loops, best of 3: 847 ns per loop

Solution 2:

How about the following (where x is your list):

[x[i:i+3] for i in range(0, len(x), 3)]

This is trivial to generalize for n!=3.

As to your second question, they're equivalent so I think it's a matter of style. However, do make sure you're not confusing append with extend.

Solution 3:

Have you heard of boltons?

Boltons is a set of pure-Python utilities in the same spirit as — and yet conspicuously missing from — the the standard library

It has what you want, built-in, called chunked

from boltons import iterutils

iterutils.chunked([1,2,3,4,5,6,7,8], 3)

Output:

[[1, 2, 3], [4, 5, 6], [7, 8]]

And whats more attractive in boltons is that it has chunked as an iterator, called chunked_iter, so you don't need to store the whole thing in memory. Neat, right?

Solution 4:

This function can take any kind of iterable (not only sequences of known length):

import itertools

defgrouper(n, it):
    "grouper(3, 'ABCDEFG') --> ABC DEF G"
    it = iter(it)
    returniter(lambda: list(itertools.islice(it, n)), [])

print(list(grouper(3, [1,2,3,4,5,6,7,8,9,10])))
# [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

Solution 5:

I think this split function does what you're looking for (though it works with any iterator rather than just lists):

from itertools import islice

deftake(n, it):
    "Return first n items of the iterable as a list"returnlist(islice(it, n))

defsplit(it, size):
    it = iter(it)
    size = int(size)
    ret = take(size, it)
    while ret:
        yield ret
        ret = take(size, it)

Edit: Regarding your asside, I always use list.append(blah), as it feels more idiomatic to me, but I believe they are functionally equivalent.

Post a Comment for "Creating Sublists"