-1

I try to slice a list in equally parts using Python. Because I want to reuse the output I want to create new lists out of the parts.

There are a lot of issues on stackoverflow on that. I decided to use pprint.

l = list(range(100))
n = 15
def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]
import pprint
pprint.pprint(list(chunks(list(range(0, 100)), 10)))

The actual result is as follows:

[[0, 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, 26, 27, 28, 29],
 ]

and so on

I expect output like

list1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list2 = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

and so on.

-> How can I automatically create this kind of lists? I don't want to manually number the list's name.

  • You may want to ask yourself why you'd want to do this, as the list of lists is a more manageable data structure than having 10 individually named lists. – Chris Apr 11 '19 at 14:14
  • Why do you want `list2 = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]` instead of `list2 = [10,11,12,13,14,15,16,17,18,19]` ? – Alain T. Apr 11 '19 at 15:19
  • @Chris I have massive data. `l` is actually a list of 25 million digits in 200'000 items. I have to process the data further, so I thought its better to separate the items in separated list. With your comment I see that it might be better to address the parts in the list of lists. I didn't know about the possibility. Have many thanks. – vizzerdrix Apr 12 '19 at 07:10
  • @AlainT this was a typical literal error. Thanks for your sharp eyes. I edited the post. – vizzerdrix Apr 12 '19 at 07:11

3 Answers3

2

You can unpack the lists:

l = list(range(100))
n = 15
def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]
import pprint

res = (list(chunks(list(range(0, 100)), 10)))
lstA, lstB, lstC, lstD, lstE, lstF, lstG, lstH, lstI, lstJ = [*res]

pprint.pprint(lstA)
pprint.pprint(lstB)

OUTPUT:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

OR

If you dont them saved:

print(res[0])
print(res[1])

OUTPUT:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
1

Seems like you already have what you want. If you do something like:

my_lists = list(chunks(list(range(0, 100)), 10))

my_lists[0] -> list1 . . .

my_lists[n] -> listn

  • This sounds very interesting. What you are saying is that I don't have to use pprint to solve my problem. I will definitely give this a try. Thank you. – vizzerdrix Apr 12 '19 at 07:20
  • pprint only help you printing the array in a convenient way. If you want to use it but if you want to be able to access every list just store it in a list of lists as shown above. – arthur vervaet Apr 12 '19 at 11:37
0

If you're only looking for a way to print the chunks, you could use a simple loop with a striding range:

l = list(range(100))
n = 10
for i in range(0,len(l),n): 
    print(f"list{1+i//n} = {l[i:i+n]}")

# output:
list1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list2 = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
list3 = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
list4 = [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
list5 = [40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
list6 = [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
list7 = [60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
list8 = [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
list9 = [80, 81, 82, 83, 84, 85, 86, 87, 88, 89]
list10 = [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

If you want actual variables (list1, list2, ...) you could use exec() in the loop but that would be rather hard to use in the rest of the program (as opposed to a list of lists):

for i in range(0,len(l),n):
    exec(f"list{1+i//n} = l[i:i+n]")

...

listOfLists = [ l[i:i+n] for i in range(0,len(l),n) ]

...

listOfLists[0] is same as list1
listOfLists[1] is same as list2 
...
listOfLists[K-1] is same as listK
Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • You are assuming well that I'm looking for a way to print the chunks. Your first example nailed it in a very elegant way. Have many thanks. This is exactly what I wanted. – vizzerdrix Apr 12 '19 at 07:24