Home > Back-end >  Better Ways to Divide a List
Better Ways to Divide a List

Time:09-21

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!

CodePudding user response:

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']

CodePudding user response:

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.

CodePudding user response:

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)]
  • Related