Divide the elements from *iterable* into *n* parts, maintaining order. >>> group_1, group_2 = divide([1, 2, 3, 4, 5, 6], 2) >>> list(group_1) [1, 2, 3] >>> list(group_2) [4, 5, 6] If the length of *iterable* is not evenly divisible by *n*, then the
(iterable, n)
| 262 | |
| 263 | |
| 264 | def divide(iterable, n) -> List[Iterator]: |
| 265 | """Divide the elements from *iterable* into *n* parts, maintaining |
| 266 | order. |
| 267 | |
| 268 | >>> group_1, group_2 = divide([1, 2, 3, 4, 5, 6], 2) |
| 269 | >>> list(group_1) |
| 270 | [1, 2, 3] |
| 271 | >>> list(group_2) |
| 272 | [4, 5, 6] |
| 273 | |
| 274 | If the length of *iterable* is not evenly divisible by *n*, then the |
| 275 | length of the returned iterables will not be identical: |
| 276 | |
| 277 | >>> children = divide([1, 2, 3, 4, 5, 6, 7], 3) |
| 278 | >>> [list(c) for c in children] |
| 279 | [[1, 2, 3], [4, 5], [6, 7]] |
| 280 | |
| 281 | If the length of the iterable is smaller than n, then the last returned |
| 282 | iterables will be empty: |
| 283 | |
| 284 | >>> children = divide([1, 2, 3], 5) |
| 285 | >>> [list(c) for c in children] |
| 286 | [[1], [2], [3], [], []] |
| 287 | |
| 288 | This function will exhaust the iterable before returning and may require |
| 289 | significant storage. If order is not important, see :func:`distribute`, |
| 290 | which does not first pull the iterable into memory. |
| 291 | |
| 292 | """ |
| 293 | if n < 1: |
| 294 | raise ValueError("n must be at least 1") |
| 295 | |
| 296 | try: |
| 297 | iterable[:0] |
| 298 | except TypeError: |
| 299 | seq = tuple(iterable) |
| 300 | else: |
| 301 | seq = iterable |
| 302 | |
| 303 | q, r = divmod(len(seq), n) |
| 304 | |
| 305 | ret = [] |
| 306 | stop = 0 |
| 307 | for i in range(1, n + 1): |
| 308 | start = stop |
| 309 | stop += q + 1 if i <= r else q |
| 310 | ret.append(iter(seq[start:stop])) |
| 311 | |
| 312 | return ret |
| 313 | |
| 314 | |
| 315 | def retry_on_specific_exceptions( |