0

I have a list of 100 values that i would like to split up to a list with arrays in it were every array holds 10 values.

What I have, for ex:

array = [1,2,3,4,5,6,7,7,8,9,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8............]

What I want, for ex:

newarray = [[0,1,2,3,4,5,6,7,7,8,9],[1,2,3,4,5,6,7,8,9,1],[2,3,4,5,6,7,8............]]

I want to do this so that I can call for newArray[4] and get 10 values.

This is what i have tried:

for i in range(len(values)):
    a = []
    for j in range(10): 
        a.append(values[j])
    val.append(a)

    print(val)

Problem is that i now get all the first ten values in every different array.

What am I doing wrong?

Thosc1
  • 108
  • 10
  • 1
    You are always appending the first ten elements since you're using `range(10)`. You can change the inner `for` loop to `a.append(values[i*10+j])` – pault Oct 18 '18 at 14:54
  • 1
    Possible duplicate of [How do you split a list into evenly sized chunks?](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – Dominique Paul Oct 18 '18 at 14:55
  • that worked! @pault thanks!! – Thosc1 Oct 18 '18 at 15:01
  • if i try to print (val) outside the for loop i get "list index out of range" do you know why this is? @pault – Thosc1 Oct 18 '18 at 15:23

3 Answers3

1

Try this, using list comprehensions:

[array[i:i+10] for i in range(0, len(array)-9, 10)]
Óscar López
  • 225,348
  • 35
  • 301
  • 374
1

There are certainly other (arguably more pythonic) ways to solve this problem, but here is what's wrong with your code:

In this part:

...
    for j in range(10): 
        a.append(values[j])
...

The index j will be the same for each value of i. The value you want to append, should be offset based on i.

Here is one approach:

...
    for j in range(10): 
        a.append(values[(i*10)+j])
...

This way when i = 0, you'll append the items from values[0] through values[9]. Likewise, when i=1, you'll append the items from values[10] through values[19] and so on.

pault
  • 37,170
  • 13
  • 92
  • 132
  • if i try to print (val) outside the for loop i get "list index out of range" do you know why this is? – Thosc1 Oct 18 '18 at 15:25
  • @Thosc1 it's hard to tell without seeing how you defined `val`. I assumed you had initialized it to an empty list before the `for` loop. Please either [edit] this question or ask a new one, but in either case make sure to include the full code and the full traceback. – pault Oct 18 '18 at 15:28
1

The reason you're only getting the first ten values is that you're only iterating over the first ten values, just for len(values) iterations. So, instead of ending up with a list of 10 sublists, each with 10 values, you get a list with 100 sublists, each with the first 10 values of the original list. To fix this, you need to change your outer loop variable to be range(0, len(values) - 10, 10) and your inner variable to be range(i, i + 10). Of course, then you could be left with a little chunk at the end which you'd have to deal with, but in your case len(values) % 10 == 0 so you should be fine there. Putting this all together, we have:

val = []
for i in range(0, len(values) - 10, 10):
    val.append(values[i: i + 10])

Doing this via list-comprehension:

val = [ values[i : i + 10] for i in range(0, len(values) - 10, 10) ]
Woody1193
  • 4,413
  • 1
  • 28
  • 58