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.
While this is not a Westminster system (but it is mostly a parliamentary one), Romania has suffered a similar process just in the recent years:

* [in 2016 the Social Democrats won the Parliamentary](https://blogs.lse.ac.uk/europpblog/2016/12/21/social-roots-romania-2016-election/) elections and formed the Goverment. Their leader (Liviu Dragnea) wanted to be the PM, but this was prevented by having been convicted of election rigging. However, he remained head of the Chamber of Deputies (part of the Parliament) which virtually means the 3rd person of the state (after the President and the head of Senate). He was [de facto running the Government](https://www.bbc.com/news/world-europe-48421430)
* in May 2019, Dragnea was [sentenced to jail and had to leave all positions](https://www.politico.eu/article/liviu-dragnea-romanian-social-democrat-leader-dragnea-sentenced-to-jail-fake-jobs/) (party leader, head of Chamber of Deputies).
* as a bonus the subsequent PM (who also became the party leader) and her government has not managed to survive a motion of trust (no-confidence vote).