2

Possible Duplicate:
How do you split a list into evenly sized chunks in Python?

I have a list such as:

L = [1,2,3,4,5,6,7,8]

Let's say I want to divide this L into 3 parts into something like:

result = [[1,2,3],[4,5,6],[7,8]]

How can I do this without using higher programming methods (ONLY USING SIMPLE PYTHON METHOD)?

Community
  • 1
  • 1
EHMJ
  • 251
  • 1
  • 4
  • 12
  • This is not homework by the way, it's part of final exam but I do not want to look at answers yet. – EHMJ Dec 11 '12 at 01:02
  • Also you cannot assume that there is 8 elements like above there can be 7 or 6 or whatever integer. – EHMJ Dec 11 '12 at 01:03
  • I've formatted this question for you but I still don't understand the last sentence. Also the three lists in your `result` list are not what I would call "equal". – johnsyweb Dec 11 '12 at 01:20

2 Answers2

1

You can try something like:

In [13]: l = [1,2,3,4,5,6,7,8]

In [14]: result = [l[i:i+3] for i in xrange(0, len(l), 3)]

In [15]: result
Out[15]: [[1, 2, 3], [4, 5, 6], [7, 8]]

This pulls out slices from the list that are of length n, where n is the number that you add to i and is also the step value in your range. As @Eric pointed out, this breaks the list into chunks of three, but not three chunks. In order to get it into three chunks, you can do something like:

In [21]: l = [1,2,3,4,5,6,7,8]

In [22]: chunk = int(round(len(l)/3.0))

In [23]: result = [l[i:i+chunk] for i in range(0,len(l),chunk)]

In [24]: result
Out[24]: [[1, 2, 3], [4, 5, 6], [7, 8]]

In [25]: l = [1,2,3,4,5]

In [26]: chunk = int(round(len(l)/3.0))

In [27]: result = [l[i:i+chunk] for i in range(0,len(l),chunk)]

In [28]: result
Out[28]: [[1, 2], [3, 4], [5]]

As it sounds like you have certain constraints, this could be written in a for loop as well (although this actually has more function calls than the one above :) ):

In [17]: result = []

In [18]: for i in xrange(0, len(l), 3):
   ....:     result.append(l[i:i+3])
   ....:     
   ....:     

In [19]: result
Out[19]: [[1, 2, 3], [4, 5, 6], [7, 8]]
RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
  • is there way other than using result = [l[i:i+3] – EHMJ Dec 11 '12 at 01:06
  • @EricJung What are your constraints? Also, where are you running into issues? – RocketDonkey Dec 11 '12 at 01:07
  • I am trying to sliced this into 3 equal parts – EHMJ Dec 11 '12 at 01:08
  • Constraints without using any of those function calls beside for loop – EHMJ Dec 11 '12 at 01:08
  • @EricJung Hmm, well none of that (except for `range`) involves function calls - that is taking a slice from a list. Is that not allowed? – RocketDonkey Dec 11 '12 at 01:09
  • Yes without using slicing nor any of the imported program. How would you do it with only using range – EHMJ Dec 11 '12 at 01:11
  • @EricJung: What imported program? – Eric Dec 11 '12 at 01:14
  • like array or lambda stuff like that, I am a newbie, could someone please divide it without any complexity?? Thank you for your answer by the way. – EHMJ Dec 11 '12 at 01:15
  • Also, this code slices a list into chunks of 3, not 3 chunks. – Eric Dec 11 '12 at 01:15
  • OKAY I THINK I GOT IT THANKS ROCKET – EHMJ Dec 11 '12 at 01:15
  • @EricJung Hmm, no slices eh? That is a very bizarre constraint :) If you look at what it is doing, it is taking a slice of the list that starts at `i` and extends to `i+3`. To simulate this, you can have another `for` loop inside of the main loop that iterates from the current value of `i` to `i+3`, appending the list element at that position to an intermediate list, which is then added to the main list. I don't envy your task :) – RocketDonkey Dec 11 '12 at 01:16
  • @EricJung: This code does not use lambdas or np.array - what's the problem? – Eric Dec 11 '12 at 01:17
  • 2
    If you have a problem with slicing you should do the python tutorial. You'll be better off for it in the end. – monkut Dec 11 '12 at 01:20
  • @Eric Great point - updated to reflect that (my attention to detail - not so good :) ). – RocketDonkey Dec 11 '12 at 01:21
  • @EricJung No problem, good luck with the exam :) – RocketDonkey Dec 11 '12 at 01:36
1

RocketDonkey's answer should be fine. Here's one without the list comprehension:

l = [1,2,3,4,5,6,7,8]
result = {}
idx = 0
for i in l:
    group = idx/3
    if group not in result:
        result[group] = []
    result[group].append(i)
    idx += 1
result.values()


>>> [[1, 2, 3], [4, 5, 6], [7, 8]]

enumerate() can be used to remove the manual idx increment.

monkut
  • 42,176
  • 24
  • 124
  • 155