Python: How to Split a List Based on a Specific Element

Splitting a list based on specific element

The split/join technique advocated by @simre works for the data shown in the question. Here's a loop-based approach that may be more flexible:

slist = ['|', 'a', 'b', 'c', '|', '', '|', 'd', 'e', '|']

result = []
e = []

for v in slist:
if v == '|':
if e:
result.append(e)
e = []
elif v:
e.append(v)

print(result)

Output:

[['a', 'b', 'c'], ['d', 'e']]

This also works where the strings in the list are comprised of more than one character. There is an implicit reliance on the last element in the list being equal to '|'.

Split a list into sublists based on the value of an element?

Here's one way to do it:

>>> foo = ["Bob", 14, 20, "Sam", "Bob", 15, 23, "Bob", "Jim", 14]
>>>
>>> x = [i for i, s in enumerate(foo) if s == "Bob"]
>>> y = x[1:] + [len(foo)]
>>> z = [foo[i:j] for i, j in zip(x, y)]
>>>
>>> z
[['Bob', 14, 20, 'Sam'], ['Bob', 15, 23], ['Bob', 'Jim', 14]]
>>>

If the list doesn't start with 'Bob', it will skip the elements that precede the first 'Bob', so if you want those then you'd need to add a check for that.

How to split the list based on the value of the elements

Here is a solution with itertools. I used as condition to keep consecutive numbers (thanks @KarlKnechtel).

from itertools import groupby
diff = [item - i for i, item in enumerate(lst)]
[[x[1] for x in g] for i,g in groupby((zip(diff,lst)), lambda x: x[0])]

output:

[[75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88],
[118, 119, 120, 121, 122, 123, 124, 125, 126, 127],
[155, 156, 157, 158, 159, 160, 161, 162, 163, 164],
[185, 186, 187, 188, 189, 190, 191, 192, 193],
[195, 196, 197, 198, 199],
[221, 222, 223, 224, 225, 226],
[273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283],
[309, 310, 311, 312, 313, 314, 315, 316],
[371, 372, 373, 374, 375, 376, 377, 378, 379, 380],
[401, 402, 403, 404, 405, 406, 407, 408],
[439, 440]]

Splitting a List Based on a Substring

A slightly different (optimized) version of WVO's answer:

splitted = []

for item in l:
if '(Reg)' in item:
splitted.append([])
splitted[-1].append(item)

#[['1(Reg)', '100', '103', '102', '100'], ['2(Reg)', '98', '101', '100'],
# ['3(Reg)', '96', '99', '98'], ['4(Reg)', '100', '100', '100', '100'],
# ['5(Reg)', '98', '99', '99', '100'],
# ['6(Reg)', '99.47', '99.86', '99.67', '100']]

How to split a list based on new line in the list

Go over each element in the list en remove it when there is an element with length 0.

l =    ['As Michael Harvey writes, paragraphs are “in essence—a form of punctuation, and like other former. ',
'',
'Many novice writers tend to make a sharp distinction between content and style, thinking that a paper can be strong']

output = [ele for ele in l if len(ele) > 0]
list1, list2 = output

This works for your example, but it does not "split" on the empty element. Then you can use a for loop:

output = ['']

for ele in l:
if len(ele) > 0:
output[-1] += ele
else:
output.append('')

This last part splits your list:

l = ['a', 'b', '', 'c']
output = ['']

for ele in l:
if len(ele) > 0:
output[-1] += ele
else:
output.append('')
# output = ['ab', 'c']

Edit: output as lists:

l = ['a', 'b', '', 'c']
output = [[]]

for ele in l:
if len(ele) > 0:
output[-1].append(ele)
else:
output.append([])
# output = [['a', 'b'], ['c']]

If you have more lists it stops here since output can be as large as you want. If you know the result is two lists you can unpack them:

lst1, lst2 = output

sidenote: do not use variable list since it is a data structure in python.



Related Topics



Leave a reply



Submit