Re: better way to write this function



Kelie <kf9150@xxxxxxxxx> writes:

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!

See the last recipe from:
http://docs.python.org/lib/itertools-recipes.html. It's not doing
quite the same thing, but gives an illustration of one way to approach
this sort of thing.
.



Relevant Pages

  • Re: better way to write this function
    ... easier/better way. ... 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
    ... easier/better way. ... To be honest, I don't have a good understanding of ... """Divide a list into a number of lists, ...
    (comp.lang.python)
  • better way to write this function
    ... easier/better way. ... To be honest, I don't have a good understanding of ... """Divide a list into a number of lists, ...
    (comp.lang.python)
  • Wanna make lots of money?
    ... least 200 newsgroups. ... I started getting money in the mail! ... business of developing Mailing Lists. ... are tons of new honest users and new honest people who are joining the ...
    (rec.arts.comics.marvel.universe)
  • Wanna make lots of money?
    ... least 200 newsgroups. ... I started getting money in the mail! ... business of developing Mailing Lists. ... are tons of new honest users and new honest people who are joining the ...
    (soc.culture.australian)