5 examples of 'python split list into sublists' in Python

Every line of 'python split list into sublists' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
1def split_list(l, parts=1):
2 """
3 Splits a list into even pieces.
4 :param l: list
5 :param parts: number of parts
6 :return: list of lists
7 """
8 length = len(l)
9 return [l[i * length // parts: (i + 1) * length // parts]
10 for i in range(parts)]
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

14def split(alist):
15 if len(alist)>1:
16 mid = len(alist)//2
17 lefthalf = split(alist[:mid])
18 righthalf = split(alist[mid:])
19
20 return merge(lefthalf,righthalf)
21 return alist[0]
37def _itersplit(l, splitters):
38 current = []
39 for item in l:
40 if item in splitters:
41 yield current
42 current = []
43 else:
44 current.append(item)
45 yield current
286def _split_list(value):
287 """Split a string list into parts."""
288 if value:
289 return [p for p in LIST_SEP_RE.split(value) if p]
290 else:
291 return []
233def split_list(self, alist, wanted_parts=1):
234 length = len(alist)
235 return [alist[i*length // wanted_parts: (i+1)*length // wanted_parts]
236 for i in range(wanted_parts)]

Related snippets