Melbourne was Prime Minister on two occasions. The first occasion ended when he was dismissed by King William IV in 1834, the last British prime minister to be dismissed by a monarch. Six months later he was re-appointed and served for six years.
Born in London in 1779 to an aristocratic Whig family, William Lamb was the son of the 1st Viscount Melbourne and Elizabeth, Viscountess Melbourne (1751–1818), though his paternity was questioned. He was educated at Eton, Trinity College, Cambridge and the University of Glasgow. Against the background of the Napoleonic Wars, Lamb served at home as captain (1803) and major (1804) in the Hertfordshire Volunteer Infantry.
He succeeded his elder brother as heir to his father's title in 1805, and married Lady Caroline Ponsonby, an Anglo-Irish aristocrat. The following year, he was elected to the British House of Commons as the Whig MP for Leominster. For the election in 1806 he moved to the seat of Haddington Burghs, and for the 1807 election he successfully stood for Portarlington (a seat he held until 1812).
Lamb first came to general notice for reasons he would rather have avoided: his wife had a public affair with Lord Byron—she coined the famous characterisation of Byron as "mad, bad and dangerous to know". The resulting scandal was the talk of Britain in 1812. Lady Caroline published a Gothic novel, Glenarvon, in 1816; this portrayed both the marriage and her affair with Byron in a lurid fashion, which caused William even greater embarrassment, while the spiteful caricatures of leading society figures made them several influential enemies. Eventually the two were reconciled, and, though they separated in 1825, her death in 1828 affected him considerably.
In 1816, Lamb was returned for Peterborough by Whig grandee Lord Fitzwilliam. He told Lord Holland that he was committed to the Whig principles of the Glorious Revolution but not to "a heap of modern additions, interpolations, facts and fictions". He therefore spoke against parliamentary reform, and voted for the suspension of habeas corpus in 1817 when sedition was rife.