10 examples of 'split a list' in Python

Every line of 'split a list' 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
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 []
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

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)]
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]
91def split_list(alist, wanted_parts=1):
92 """Split a list to the given number of parts."""
93 length = len(alist)
94 # alist[a:b:step] is used to get only a subsection of the list 'alist'.
95 # alist[a:b] is the same as [a:b:1].
96 # '//' is an integer division.
97 # Without 'from __future__ import division' '/' would be an integer division.
98 return [ alist[i*length // wanted_parts: (i+1)*length // wanted_parts]
99 for i in range(wanted_parts) ]
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)]
61def split(args):
62 chunks = []
63 for chunk in args.chunks:
64 chunk['__threads'] = 1
65 chunk['__mem_gb'] = 6
66
67 if args.initial_reads is None:
68 chunk['chunk_initial_reads'] = None
69 else:
70 chunk['chunk_initial_reads'] = args.initial_reads / len(args.chunks)
71
72 # Combine the per-chunk subsample rates with the toplevel rate
73 chunk['chunk_subsample_rate'] = chunk.pop('subsample_rate') * args.subsample_rate
74
75 # NOTE: Here we implicitly convert SETUP_CHUNKS' output to Martian "chunk" definitions.
76 # Keys coming from SETUP_CHUNKS can override the stage-level args and are not
77 # explicitly typed by Martian. This is bad.
78
79 chunks.append(chunk)
80 join = {
81 '__mem_gb': 8,
82 }
83 return {'chunks': chunks, 'join': join}
33@staticmethod
34def split_list(alist, wanted_parts=1):
35 """
36 http://stackoverflow.com/questions/752308/split-list-into-smaller-lists
37
38 """
39 length = len(alist)
40 return [alist[i*length // wanted_parts: (i+1)*length // wanted_parts]
41 for i in range(wanted_parts)]
126def split_string(input_string):
127 '''Split a string to a list and strip it
128 :param input_string: A string that contains semicolons as separators.
129 '''
130 string_splitted = input_string.split(';')
131 # Remove whitespace at the beginning and end of each string
132 strings_striped = [string.strip() for string in string_splitted]
133 return strings_striped
33def to_list(a, delimiter=',', mapping=None):
34 l = a.split(delimiter)
35 if mapping:
36 l = [mapping[li] for li in l]
37
38 return l
101def test_splitlist(self):
102
103 l = split_list_type(List, ", \t\n\r")("1,2, 3,,, \n\r\t4")
104 self.assertEqual(l, ['1', '2', '3', '4'])
105 self.assertEqual(l, "1,2,3,4")
106 self.assertEqual(l, "1 2 3 4")
107 self.assertEqual(str(l), "1,2,3,4")
108
109 l += "7, 8"
110 self.assertEqual(l, ['1', '2', '3', '4', '7', '8'])
111
112 l -= "2, 3"
113 self.assertEqual(l, ['1', '4', '7', '8'])
114
115 l -= "5"
116 self.assertEqual(l, ['1', '4', '7', '8'])
117
118 l.extend_front("10,12")
119 self.assertEqual(l, ['10', '12', '1', '4', '7', '8'])
120
121 l.extend("0,-1")
122 self.assertEqual(l, ['10', '12', '1', '4', '7', '8', '0', '-1'])

Related snippets