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 2007 the Prime Minister of Australia [John Howard](https://en.wikipedia.org/wiki/John_Howard) lost his seat, but his party also lost the election. He is the second Australian Prime Minister to lose his sear in an election after [Stanley Bruce](https://en.wikipedia.org/wiki/Stanley_Bruce) in [1929](https://en.wikipedia.org/wiki/Australian_federal_election,_1929).

In Britain Prime Minister [Arthur Balfour](https://en.wikipedia.org/wiki/Arthur_Balfour) lost his seat of Manchester East in the [1906 general election](https://en.wikipedia.org/wiki/United_Kingdom_general_election,_1906), but remained Conservative party leader, without a seat.

The best example of a Prime Minister who lost his seat but remained prime minister is [William Lyon Mackenzie King](https://en.wikipedia.org/wiki/William_Lyon_Mackenzie_King) who lost his seat in in the [1925 Canadian general election](https://en.wikipedia.org/wiki/Canadian_federal_election,_1925) and in the [1945 Canadian general election](https://en.wikipedia.org/wiki/Canadian_federal_election,_1945), but still kept the job as Prime Minister of Canada. He returned to parliament shortly after each of these elections by persuading a member of his own party to resign from a safe seat (which triggered a by-election) where he stood in turn and won.

*Credit to [Steve Melnikoff](https://politics.stackexchange.com/users/300/steve-melnikoff) for point regarding how King returned to parliament in Canada*
While not Parliamentary, this is close enough. [Newt Gingrich](https://en.wikipedia.org/wiki/Newt_Gingrich), who was the US Speaker of the House, lost his position 1998 after his party lost only 5 seats. While that was not enough to cost them the majority (i.e. they still won overall), it was enough to spark a revolt within the party against a weak election showing they blamed him for. Gingrich, rather than lose an election for Speaker, resigned.

Also, [Eric Cantor](https://en.wikipedia.org/wiki/Eric_Cantor), House Majority Leader, neglected working hard in the primaries and wound up losing to a political newcomer, who then went on to win Cantor's seat in the 2014 Republican landslide.