British Prime Minister Rishi Sunak's governing Conservatives lost two strategically important parliamentary seats on Friday, but unexpectedly retained Boris Johnson's old constituency in a setback for the opposition Labour Party.

The by-elections were one of the last electoral tests before a national election expected next year and were seen as an indicator of the two main parties' prospects at a time when voters are struggling with high inflation, strikes and rising mortgage rates.

Sunak could draw some relief as his party narrowly avoided losing all three seats. But the problems he faces were highlighted by the loss of the formerly safe Conservative parliamentary seat of Selby and Ainsty, where Labour overturned the biggest Conservative majority at a by-election since World War Two. Labour leader Keir Starmer said the victory showed "just how powerful the demand for change is."