![]() |
|
[Tut] How to Split a List Into Evenly-Sized Chunks? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Split a List Into Evenly-Sized Chunks? (/thread-94539.html) |
[Tut] How to Split a List Into Evenly-Sized Chunks? - xSicKxBot - 04-14-2020 How to Split a List Into Evenly-Sized Chunks? In this article, I’ll show you how to divide a list into equally-sized chunks in Python. Step-by-step, you’ll arrive at the following great code that accomplishes exactly that: https://repl.it/@finxter/listchunking2?lite=true You can play around with the code yourself but if you need some explanations, read on because I’ll explain it to you in much detail: Chunking Your List Let’s make this question more palpable by transforming it into a practical problem: Problem: Imagine that you have a temperature sensor that sends data every 6 minutes, which makes 10 data points per hour. All these data points are stored in one list for each day. Now, we want to have a list of hourly average temperatures for each day—this is why we need to split the list of data for one day into evenly sized chunks. Solution: To achieve this, we use a for-loop and Python’s built-in function Code: range()The Code: range()
In our problem, our chunks have a length of 10, the start value is 0 and the max value is the end of the list of data. Putting all together: Calling Code: range(0, len(data), 10)For one single day, we have a data length of 24 * 10 = 240, so the call of the range function would be this: Code: range(0, 240, 10)So what do we have now? The start indices of each chunk and also the length – and that’s all we need to slice the input data into the chunks we need. The slicing operator takes two or three arguments separated by the colon Code: :If you want to know more about slicing read our detailed article here. A first draft of our code could be this: Code: data = [15.7, 16.2, 16.5, 15.9, ..., 27.3, 26.4, 26.1, 27.2]
chunk_length = 10 for i in range(0, len(data), chunk_length): print(data[i:i+chunk_length])Play with this code in our interactive Python shell: https://repl.it/@finxter/chunklist?lite=true However, we can still improve this code and make it reusable by creating a generatorout of it. Chunking With Generator Expressions A generator is a function but instead of a return statement it uses the keyword Code: yieldThe keyword Code: yieldCode: data = [15.7, 16.2, 16.5, 15.9, ..., 27.3, 26.4, 26.1, 27.2]
chunk_length = 10 def make_chunks(data, length): for i in range(0, len(data), length): yield data[i:i + length] for chunk in make_chunks(data, chunk_length): print(chunk)That looks already pretty pythonic and we can reuse the function Code: make_chunks()Let’s finish the code so that we get a list of hourly average temperatures as result. Code: import random def make_chunks(data, length): for i in range(0, len(data), length): yield data[i:i + length] def process(chunk): return round(sum(chunk)/len(chunk), 2) n = 10
# generate random temperature values
day_temperatures = [random.random() * 20 for x in range(24 * n)]
avg_per_hour = [] for chunk in make_chunks(day_temperatures, n): r = process(batch) avg_per_hour.append(r) print(avg_per_hour)And that’s it, this cool pythonic code solves our problem. We can make the code even a bit shorter but I consider this code less readable because you need to know really advanced Python concepts. Code: import random make_chunks = lambda data, n: (data[i:i + n] for i in range(0, len(data), n))
process = lambda data: round(sum(data)/len(data), 2) n = 10
# generate random temperature values
day_temperatures = [random.random() * 20 for x in range(24 * n)]
avg_per_hour = [] for chunk in make_chunks(day_temperatures, n): r = process(batch) avg_per_hour.append(r) print(avg_per_hour)So, what did we do? We reduced the helper functions to lambda expressions and for the generator function we use a special shorthand – the parenthesis. Summary To sum up the solution: We used the range function with three arguments, the start value, the stop value and the step value. By setting the step value to our desired chunk length, the start value to 0 and the stop value to the total data length, we get a range object containing all the start indices of our chunks. With the help of slicing we can access exactly the chunk we need in each iteration step. Where to Go From Here? Want to start earning a full-time income with Python—while working only part-time hours? Then join our free Python Freelancer Webinar. It shows you exactly how you can grow your business and Python skills to a point where you can work comfortable for 3-4 hours from home and enjoy the rest of the day (=20 hours) spending time with the persons you love doing things you enjoy to do. Become a Python freelancer now! |