python - How to to split a list at a certain value -
given unique valued list such [5,4,9,2,1,7,'dog',9] there way split a value? ie [5,4,9,2,7,'dog'].split(4) = [5,4],[9,2,7,'dog'] [5,4,9,2,7,'dog'].split(2) = [5,4,9,2], [7,'dog'] ? >>> mylist = [5,4,9,2,7,'dog'] >>> def list_split(l, element): ... if l[-1] == element: ... return l, [] ... delimiter = l.index(element)+1 ... return l[:delimiter], l[delimiter:] ... >>> list_split(mylist, 4) ([5, 4], [9, 2, 7, 'dog']) >>> list_split(mylist, 2) ([5, 4, 9, 2], [7, 'dog']) >>> list_split(mylist, 'dog') ([5, 4, 9, 2, 7, 'dog'], [])