Split list into n chunks python
Use numpy Show
or
You can also use numpy.split but that one throws in error if the length is not exactly divisible. answered Apr 16, 2015 at 15:34
6 The solution(s) below have many advantages:
The code above produces
the below output for
If you need the chunks to be sequential instead of striped use this:
Which for
See this stackoverflow link for more information on the advantages of generators. answered Feb 21, 2019 at 8:47
1 If order doesn't matter:
answered May 11, 2017 at 17:29
Output
As shown, if the iterable is not evenly divisible, the remaining items are distributed from the first to the last chunk. See more about the answered Aug 28, 2017 at 23:55
pylangpylang 36.3k11 gold badges120 silver badges110 bronze badges My answer is to simply use python built-in Slice:
rpb 2,7431 gold badge20 silver badges43 bronze badges answered Aug 18, 2020 at 10:18
ArashsyhArashsyh 55910 silver badges16 bronze badges 1 Try this:
It returns an iterator instead of a list for efficiency (I'm assuming you want to loop over the chunks), but you can replace that with a list comprehension if you want. When the number of items is not divisible by number of chunks, the last chunk is smaller than the others. EDIT: Fixed second example to show that it doesn't handle one edge case answered Jun 30, 2014 at 6:35 I came up with the following solution:
For example:
Output:
As you can see, the output consists from How it works?The trick is to use list slice step (the number after two semicolons)
and to increment the offset of stepped slicing. First, it takes every answered Apr 19 at 10:30
Vlad HavriukVlad Havriuk 1,16114 silver badges26 bronze badges Hint:
eyllanesc 226k18 gold badges130 silver badges198 bronze badges answered Aug 17, 2018 at 17:48
MitendraMitendra 1,33016 silver badges10 bronze badges Here take my 2 cents..
answered Aug 30, 2019 at 18:51
pista329pista329 6317 silver badges13 bronze badges One way would be to make the last list uneven and the rest even. This can be done as follows:
answered Jun 30, 2014 at 5:18
Sukrit KalraSukrit Kalra 31.5k7 gold badges65 silver badges70 bronze badges 4
Assuming you want to divide into n chunks:
This method simply divides the length of the list by the number of chunks and, in case the length is not a multiple of the number, adds the extra elements in the last list. answered Jun 30, 2014 at 5:54
user2963623user2963623 2,2471 gold badge13 silver badges25 bronze badges 0 If you want to have the chunks as evenly sized as possible:
Test case:
answered Feb 25, 2018 at 7:54
1 In cases, where your list contains elements of different types or iterable objects that store values of different types (f.e. some elements are integers, and some are strings), if you use
If you would like to have initial types of elements in chunks after splitting of list, you can modify
source code of
answered Jan 20, 2020 at 20:23
Eduard IlyasovEduard Ilyasov 3,1182 gold badges18 silver badges18 bronze badges This solution is based on the zip "grouper" pattern from the Python 3 docs. The small addition is that if N does not divide the list length evenly, all the extra items are placed into the first chunk.
Example usage:
The advantages of this solution are that it preserves the order of the original list, and is written in a functional style that lazily evaluates the list only once when called. Note that because it returns an iterator, the result can only be consumed once. If you want the convenience of a non-lazy list, you can wrap the result in
answered May 18, 2020 at 23:55
I would simply do (let's say you want n chunks)
It works and it's only 2 lines. Example:
And if you want a list of list:
answered Jan 27, 2021 at 16:16
2
answered Jun 30, 2014 at 5:21
sundar natarajsundar nataraj 8,3162 gold badges31 silver badges44 bronze badges
Output:
4b0 20.8k30 gold badges92 silver badges137 bronze badges answered Nov 13, 2019 at 9:33
1 This function will return the list of lists with the set maximum amount of values in one list (chunk).
Example:
output:
answered Oct 15, 2020 at 14:21
This accepts generators, without consuming it at once. If we know the size of the generator, the binsize can be calculated by
answered May 6 at 15:53
dawiddawid 6003 silver badges10 bronze badges For people looking for an answer in python 3(.6) without imports.
answered Dec 20, 2018 at 12:23
BrohmBrohm 1231 silver badge4 bronze badges 1 How do you split a list into evenly size chunks?The easiest way to split list into equal sized chunks is to use a slice operator successively and shifting initial and final position by a fixed number.
How do you split items in a list in Python?To split the elements of a list in Python: Use a list comprehension to iterate over the list. On each iteration, call the split() method to split each string. Return the part of each string you want to keep.
How do you split a list in Python 3?The split() method returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified), optionally limiting the number of splits to num.
|