Let\'s say you have a situation like this:
_, _, _, substring_1, _, substring_2 = some_string.split(\',\')
Is there a shorthand way of expressi
You could just use str.rsplit with a limit:
>>> s = 'a,b,c,d,e,f'
>>> s.rsplit(',', 3) # i.e. split on at most three commas, from the right
['a,b,c', 'd', 'e', 'f']
>>> _, d, _, f = s.rsplit(',', 3)
>>> d
'd'
>>> f
'f'
If you upgrade to Python 3.x, you can use *_
to absorb an arbitrary number of elements (you'll get a SyntaxError
in 2.x, though):
>>> *_, d, _, f = s.split(',')
>>> d
'd'
>>> f
'f'