Re: better way to write this function



On Nov 26, 9:42 am, Kelie <kf9...@xxxxxxxxx> wrote:
Hello,

This function does I what I want. But I'm wondering if there is an
easier/better way. To be honest, I don't have a good understanding of
what "pythonic" means yet.

def divide_list(lst, n):
"""Divide a list into a number of lists, each with n items. Extra
items are
ignored, if any."""
cnt = len(lst) / n
rv = [[None for i in range(n)] for i in range(cnt)]
for i in range(cnt):
for j in range(n):
rv[i][j] = lst[i * n + j]
return rv

Thanks!

x = ['1', '2', '3', '4', '5', '6', '7', '8']
def divide_list(lst, n):
rv = []
for i in range(int(round((len(lst)/n),0))):
rv.append(lst[i*n:(i+1)*n])
return rv

tmp = divide_list(x, 3)
tmp
[['1', '2', '3'], ['4', '5', '6']]

One way to do it.
.



Relevant Pages

  • Re: better way to write this function
    ... To be honest, I don't have a good understanding of ... def divide_list: ... """Divide a list into a number of lists, ...
    (comp.lang.python)
  • Re: better way to write this function
    ... To be honest, I don't have a good understanding of ... def divide_list: ... """Divide a list into a number of lists, ...
    (comp.lang.python)
  • Re: 1 NF
    ... Oh, and anothe for lists. ... bags or lists simplies other things. ... least nested sets are possible because NF2 takes its queues from the ... by Def MV ==> NFNF aka NF2 ...
    (comp.databases.theory)
  • Re: [QUIZ] VCR Program Manager (#101)
    ... thought - oh, can't do lists, no pointers - but then I realized that ... def advance ... new_node.set_next @head ...
    (comp.lang.ruby)
  • Re: Efficient Rank Ordering of Nested Lists
    ... lists may be accomplished via: ... def rankList: ... If you find the for-clause too rich in functionality, ... Agreed that dict() approach looks promising. ...
    (comp.lang.python)