

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you split a list into evenly sized chunks in Python?
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.
In following example, a list with 12 elements is present. We split it into 3 lists each of length 4
l=[10,20,30,40,50,60,70,80,90,100,110,120] x=0 y=12 for i in range(x,y,4): x=i print (l[x:x+4]) [10, 20, 30, 40] [50, 60, 70, 80] [90, 100, 110, 120]
- Related Questions & Answers
- How to split a vector into chunks in R?
- Break a list into chunks of size N in Python
- How do you turn a list into a Set in Java?
- How do you convert a list collection into an array in C#?
- Python - Split list into all possible tuple pairs
- How do you copy a list in Java?
- How do you create a list in Java?
- How do you make a list iterator in Java?
- How do I split a multi-line string into multiple lines?
- Java Program to Split a list into Two Halves
- How do you turn an ArrayList into a Set in Java?
- Splitting an array into chunks in JavaScript
- How do you create a list with values in Java?
- How do you create a list from a set in Java?
- Custom list split in Python
Advertisements