Since you are on Python 3, this is easy. PEP 3132 introduced a welcome simplification of syntax when assigning tuples - extended iterative unpacking. Previously, when assigning variables in a tuple, the number of elements to the left of the assignment must be exactly the same as the number to the right.
In Python 3, we can designate any variable on the left as a list by first specifying an asterisk *. This will capture as many values as possible, while still filling in the variables on the right (so it should not be the rightmost element). This avoids many unpleasant fragments when we do not know the length of the tuple.
a, *b = "foo".split(":") print("a:", a, "b:", b)
gives:
a: foo b: []
EDIT the following comments and discussions:
Compared to the Perl version, this is significantly different, but it is Python (3). Compared to the Perl version, re.split() will be more similar, however, accessing the RE mechanism to split around a single character is an extra overhead.
With a few elements in Python:
s = 'hello:world:sailor' a, *b = s.split(":") print("a:", a, "b:", b)
gives:
a: hello b: ['world', 'sailor']
However, in Perl:
my $s = 'hello:world:sailor'; my ($a, $b) = split /:/, $s; print "a: $ab: $b\n";
gives:
a: hello b: world
You can see that additional elements are ignored or lost in Perl. This is pretty easy to replicate in Python if required:
s = 'hello:world:sailor' a, *b = s.split(":") b = b[0] print("a:", a, "b:", b)
So the equivalent of a, *b = s.split(":") in Perl would be
my ($a, @b) = split /:/, $s;
NB: we should not use $a and $b in general Perl, since they are of particular importance when used with sort . I used them here for consistency with the Python example.
Python has an extra trick up its sleeve; we can unpack any element in the tuple on the left:
s = "one:two:three:four" a, *b, c = s.split(':') print("a:", a, "b:", b, "c:", c)
gives:
a: one b: ['two', 'three'] c: four
While in the Perl equivalent, the array ( @b ) is greedy, and the scalar $c is undef :
use strict; use warnings; my $s = 'one:two:three:four'; my ($a, @b, $c) = split /:/, $s; print "a: $ab: @bc: $c\n";
gives:
Use of uninitialized value $c in concatenation (.) or string at gash.pl line 8. a: one b: two three four c: