easiest way to split a list into evenly divisble smaller lists, and assign them to variables?

flamesrock flamesrock at gmail.com
Mon Jun 6 02:52:04 EDT 2005


Lets say I have a list containing 12, 13, 23 or however many entries.
What I want is the greatest number of lists evenly divisible by a
certain number, and for those lists to be assigned to variables.

This leads to a few problems..

If I don't know the length of the list beforehand, I can't create the
variables and hardcode them. Is it possible in python to generate free
variables without stating them explicitly in the code, and if so, how
would you get a reference to them?

Secondly, is there an easier way to chop a list up into smaller lists?

consider this code:
#        my_list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,
#                 15,16,17,18,19,20,21,22,23,24,25]
#        entry=0
#        dictionary_of_lists = {}
#        while len(original_list) != 0:
#            new_list=[]
#            for x in range(4):
#                if len(original_list) > 0:
#                    new_list.append(files.pop(0))
#            dictionary_of_lists[entry] = new_list
#            entry +=1

would give us
{1:[1,2,3,4],2:[5,6,7,8],
3:[9,10,11,12],4:[13,14,15,16],
5:[17,18,19,20],6:[21,22,23,24],7:[25]}

Is there a better way? What I'd like to do is create free variables
without needing to put them in a dictionary.

If not, is it possible in other languages?

-thank in advance




More information about the Python-list mailing list