Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating sublists [duplicate]

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]         if len(sub)==n: result+=[sub] ; sub=[]     if sub: result+=[sub]     return result 

An example:

If the list is:

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

And n is:

3 

Return:

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

Is there a more eloquent / concise way?

An aside, what is preferred when appending lists to lists (in the context above):

list1+=[list2] 

Or:

list1.append(list2) 

Given that (according to Summerfeild's 'Programming in Python 3') they are the same?

Thanks.

like image 537
Michael Puckett Avatar asked Dec 21 '10 16:12

Michael Puckett


People also ask

Can you have duplicate values in a list?

What are duplicates in a list? If an integer or string or any items in a list are repeated more than one time, they are duplicates.

How do you make all Sublists in Python?

Step 1 : given a list. Step 2 : take one sublist which is empty initially. Step 3 : use one for loop till length of the given list. Step 4 : Run a loop from i+1 to length of the list to get all the sub arrays from i to its right.


1 Answers

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 
like image 197
unutbu Avatar answered Oct 05 '22 21:10

unutbu