Efforts to increase female political representation are often thought to be at odds with meritocracy. This paper develops a theoretical framework and an empirical analysis to examine this idea. We show how the survival concerns of a mediocre male party leadership can create incentives for gender imbalance and more incompetent men in office. The predictions are tested with data on candidates in Swedish municipalities over seven elections (1988-2010), where we use administrative data on labor-market performance to craete a measure of the competence of politicians. We investigate the effects of the "zipper" quota, requiring party groups to alternate male and female names on the ballot, unilaterally implemented by the Social Democratic party in 1993. Far from being at odds with meritocracy, this quota increased the competence of male politicians where it raised the share of female representation the most.