top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Splitting a list into even size chunks in python?

0 votes
1,191 views

Hi,

I have a list of arbitrary length, and I need to split it up into equal size chunks. There are some obvious ways to do this, like keeping a counter and two lists, and when the second list fills up, add it to the first list and empty the second list for the next round of data, but this is potentially extremely expensive.

I was wondering if anyone had a good solution to this for lists of any length

This should work:

l = range(1, 1000)
print chunks(l, 10) -> [ [ 1..10 ], [ 11..20 ], .., [ 991..999 ] ]

I was looking for something useful in itertools but I couldn't find anything obviously useful.

Appretiate your help.

posted Mar 27, 2013 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Also - what is the chunk size? Do you know it in advance? Is it fixed or calculated from the data?

1 Answer

0 votes

Look again, for the grouper() recipe. For lists you can also use slicing:

>>> items
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> n = 3
>>> [items[start:start+n] for start in range(0, len(items), n)]
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g']]
answer Mar 27, 2013 by anonymous
Grouper() is good tty.cooked() with just a little time.time() and crypt.salt()
Similar Questions
+1 vote

For example:

a=[-15,-30,-10,1,3,5]

I want to find a negative and a positive minimum.

example: negative
print(min(a)) = -30

positive
print(min(a)) = 1
0 votes

I have a list, songs, which I want to divide into two groups.
Essentially, I want:

new_songs = [s for s in songs if s.is_new()]
old_songs = [s for s in songs if not s.is_new()]

but I don't want to make two passes over the list. I could do:

new_songs = []
old_songs = []
for s in songs:
 if s.is_new():
 new_songs.append(s)
 else:
 old_songs.append(s)

Which works, but is klunky compared to the two-liner above. This seems like a common enough thing that I was expecting to find something in itertools which did this. I'm thinking something along the lines of:

matches, non_matches = isplit(lambda s: s.is_new, songs)

Does such a thing exist?

0 votes

I want to create a random float array of size 100, with the values in the array ranging from 0 to 5. I have tried random.sample(range(5),100) but that does not work. How can i get what i want to achieve?

+2 votes

How we can send mail with attachment in Python? Is it any prerequisite for it?

+2 votes

I want to use the stdout of child process as an input of another thread, but some how I am not able to read the stdout. Using Popen I have created a child process and stdout of it I have redirected to PIPE (because I don't want that to be printed on with my main process). Now using this statement,"Line=Proc.stdout.readline()" I want to read stdout of child process and I have to do operation on that. but somehow i am not able to read from stdout of child process.

Following is the code snapshot -

Proc = Popen(["python.exe","filename.py"],stdout=PIPE)

while True:
            Line = Proc.stdout.readline()
            print Line

Here,
Filename.py(Child process) is continuously printing "Hello World!!" in place of reading stdout of child process.

...