473,507 Members | 4,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Jul 19 '05 #1
3 3144
"flamesrock" <fl********@gmail.com> writes:
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.
You almost certainly don't want to do that. That's something
beginning programmers often think of doing, but it's almost always
better to use something more general, like an array.
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:
Umm, what's "original_list"? Do you mean "my_list"? The rest of
your code has similar errors.
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.


Your best bet is probably to create a list of lists:

sublist_length = 4 # desired length of the "inner" lists
list_of_lists = []
for i in xrange(0, len(my_list), sublist_length):
list_of_lists.append(my_list[i: i+sublist_length])

That gives list_of_lists =

[[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]]

So you'd just use list_of_lists[0] to refer to the 1st sublist, etc.
Jul 19 '05 #2
hmmm..that makes much more sense. thanks :)

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5784
by: simpleman | last post by:
Hi, I have an assignment that requires me to subtract two very large unsigned integers using linked list.AFAIK I have to manipulate data as character. But I dont know how to get input into the...
10
2408
by: randomtalk | last post by:
hello, i have another problem i feel that i have to be missing something.. Basically, i've written a recursive function to find all the prime up to a number (lim).. here is the function: The...
5
3341
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
10
1399
by: marcstuart | last post by:
How do I divide a list into a set group of sublist's- if the list is not evenly dividable ? consider this example: x = y = 3 # number of lists I want to break x into z = y/x what I...
0
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.