0

I have a list that contains 100 strings, which looks like this below.

['boy',
 'boy1',
   :
 'end']

I am trying to divide my list into 5 different lists. Here is my approach.

#Divide my list into 5 lists, which contain 20 strings for each list
chunks = [list1[x:x+20] for x in range(0, len(list1), 20)]

#Extract the list form the chunks
a1 = chunks[0:1]
a2 = chunks[1:2]
a3 = chunks[2:3]
a4 = chunks[3:4]
a5 = chunks[4:5]

#Extract list from list a1 ~ a5, since it has double parentheses
output1 = [elem for output_list in a1 for elem in output_list]
output2 = [elem for output_list in a2 for elem in output_list]
output3 = [elem for output_list in a3 for elem in output_list]
output4 = [elem for output_list in a4 for elem in output_list]
output5 = [elem for output_list in a5 for elem in output_list]

Since it is a small list with only 100 strings. But if I need to deal with a larger list, for example, a list that contains 1000 or 100000 strings, it will take a long time for me to divide the list into different lists.

I am wondering are there any better ways for me to divide a list into any numbers that I prefer?

Thank you so much!

CodingStark
  • 199
  • 3
  • 17
  • 2
    Does this answer your question? [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) – Niel Godfrey Pablo Ponciano Sep 20 '21 at 05:25
  • @NielGodfreyPonciano Not really. I tried the code. It works well but I still do not know how to extract the result/output into 5 different lists. The result from the code will be a list that contains 5 lists. – CodingStark Sep 20 '21 at 05:31

3 Answers3

1

You could use the numpy.array_split() function for this.The function takes 2 arguments , the list to be divided and the number of sublists required.

import numpy as np

#List of length 20
a_list = ["boy1","boy2","boy3","boy4","boy5","boy6","boy7","boy8","boy9","boy10","boy11","boy12","boy13","boy14","boy15","boy16","boy17","boy18","boy19","end"]

#Using the split function to divide my list into 5 sublists of equal size
splits = np.array_split(a_list, 5)

#Output of the above line will be a list , which contains numpy arrays that have to be made into separate lists 

i=0
for array in splits:
    i=i+1
    exec(f'output{i} = list(array)') #Assigning a name to each sublist
    
print("output1:",output1)
print("output2:",output2)
print("output3:",output3)
print("output4:",output4)
print("output5:",output5)

The output is as follows (shows the 5 sub lists each containing 4 strings):

output1: ['boy1', 'boy2', 'boy3', 'boy4']
output2: ['boy5', 'boy6', 'boy7', 'boy8']
output3: ['boy9', 'boy10', 'boy11', 'boy12']
output4: ['boy13', 'boy14', 'boy15', 'boy16']
output5: ['boy17', 'boy18', 'boy19', 'end']
Nilaya
  • 148
  • 8
1

If you want to have a variable number of chunks N then you cannot have output1, output2, ..., outputN, since you don't know N in advance. So you will need a list of lists, or a dictionary of index:list pairs, or whatever, and, if you really need to, create your variables from there.

But let's keep to a fixed number of chunks: you copy your list elements thrice, and this unnecessarily slows down your code. You may at least skip the a1 = chunks[0:1] etc passages and just do output1 = chunks[0] etc; but even the chunks are not needed:

output1 = [e for e in list1[0:20]

is enough.

gimix
  • 3,431
  • 2
  • 5
  • 21
1

Simple one liner code

ls = list(range(1,101))
chunk = 20
a1,a2,a3,a4,a5 = *[ls[i:i+chunk] for i in range(0,len(ls),chunk)]
Shubham Gupta
  • 66
  • 1
  • 6