Question: In a Parliamentary democracy, general elections appoint constituency representatives. Most such appointees are members of parties with internally determined leaders. One or more parties then form a government, with one of these party leaders the head of government.

However, it's theoretically possible that a party that enters or stays in government due to its electoral performance has to change leader, due to that individual losing their seat. (Party leaders typically come from safe seats to prevent this.)

Has this ever actually happened? I'd be interested if this happened in the UK, or failing that in any nation with a Westminster system.

Answer: 
In the 2013 British Columbia provincial election, [Christy Clark](https://en.wikipedia.org/wiki/Christy_Clark), the premier, lost her own seat to David Eby, even though the BC Liberal Party won a majority of 49 of the 85 seats in the legislature.

As a result, Ben Stewart resigned from his safe Liberal riding of Westside-Kelowna, effectively yielding his seat to her via a by-election. Clark resumed her role as premier, then appointed Stewart as a provincial representative to Beijing.
Something like *nearly* happened in North-Rhine-Westphalia in Germany [recently](https://en.wikipedia.org/wiki/North_Rhine-Westphalia_state_election,_2017), where the candidate for prime minister of the winning party, Armin Laschet, won his seat by only a slim margin ([article in German](https://www.merkur.de/politik/nrw-wahl-2017-cdu-spitzenkandidat-laschet-gewinnt-direktmandat-zr-8308380.html)).

If he had lost it, the party would have had to do some shenanigans to get him into parliament (e.g. some other winning candidate would have had to renounce his seat) since he must be a member of parliament to become prime minister.