* Lopez Obrador to be first leftist president in decades
* Leads rivals by over 30 points, with more than half the vote
* Trump tweets congratulations to Lopez Obrador (Adds results, celebrations)
By Christine Murray and Diego Oré
MEXICO CITY, July 1 (Reuters) - Andres Manuel Lopez Obrador won Mexico's presidency in a landslide victory on Sunday, setting the stage for the most left-wing government in the country's democratic history at a time of tense relations with the Trump administration.
The 64-year-old former Mexico City mayor won with the widest margin in a presidential election since the 1980s, according to an official quick count that showed him taking more than half the vote -- some 30 points ahead of his nearest rival.
Pledging to eradicate corruption and subdue drug cartels with a less confrontational approach, Lopez Obrador will carry high expectations into office, while his efforts to reduce inequality will be watched closely by nervous investors.
His government could usher in greater scrutiny of foreign investment and a less accommodating approach to the United States.
The peso whipsawed through the day, strengthening then falling against the dollar after the scale of his victory became clear.
Investors are closely watching to see whether his four-year-old MORENA party ends up with a majority in Congress, a result that would allow him more freedom to change economic policy.
Rivals Ricardo Anaya, a former head of the center-right National Action Party (PAN), and ruling Institutional Revolutionary Party (PRI) candidate Jose Antonio Meade, conceded defeat within minutes of exit polls.
Tens of thousands of people packed Mexico City's vast Zocalo city square, where Lopez Obrador spoke after midnight, flanked by his wife and children.
"The new project of the nation will try to seek an authentic democracy," he said, in a conciliatory speech promising central bank independence and economic prudence, along with respect for individual freedoms.
"I want to go down in history as a good president of Mexico," he said.
In Lopez Obrador's small hometown of Tepetitan, in the southern state of Tabasco, neighbors gathered in the village square, music blared, children rode bikes with cans tied to them and motorists honked their horns in jubilation.
Josefa del Carmen Paz Reyes, a 68-year-old lawyer, cheered the victory of her childhood friend.
"I am an older person, and I feel so happy I could dance," said Paz Reyes.
"It is unprecedented, everything that has happened in Tabasco, in Mexico. We were waiting for this change and now we have it."
WORKING WITH TRUMP
Lopez Obrador's nationalism, stubborn nature and put-downs of rivals have drawn comparisons to U.S. President Donald Trump.
In a posting on Twitter, Trump congratulated the leftist on his victory.
"I look very much forward to working with him. There is much to be done that will benefit both the United States and Mexico!" Trump tweeted.
The first high-level contact between Lopez Obrador and the White House is likely to be a phone call on Monday. Earlier on Sunday, Trump raised the prospect of taxing cars imported from Mexico if there are tensions with the new government.
The United States, which has been at odds with Mexico and Canada over the renegotiation of the North American Free Trade Agreement (NAFTA), has launched a probe into whether to slap tariffs on imported autos. Results are expected within months.
How Lopez Obrador handles relations with Trump, who has also sparred with Mexico over the U.S. president's call for a wall on the U.S.-Mexican border, will help define the new administration which will take office on Dec. 1.
Once an agitator famously photographed in a blood-splattered shirt after being hit with a truncheon at a protest, Lopez Obrador has mellowed with age and picked a team to reassure investors that his plans will not roil the economy.
"We have witnessed today a very profound reshaping of the country's political map," Goldman Sachs economist Alberto Ramos said. "The balance of power at the federal and local levels has definitely shifted to the left, with unclear implications for the near-term policy direction."
Lopez Obrador, who hails from an impoverished part of Mexico where oil exploitation began, reiterated a vow to review for signs of corruption contracts issued to private energy firms under the current government.
LONG ROAD TO VICTORY
Lopez Obrador first came to global attention as Mexico City mayor, a post he left to run for president in 2006. Narrowly losing, he cried fraud and launched street protests many thought would end his political career.
However, with stubborn self-belief, he began a long journey back to prominence, tirelessly visiting far-flung villages and towns neglected by mainstream politicians for decades.
His victory is a stinging rebuke to the PRI, which has governed Mexico for 77 of the past 89 years, and its conservative rival, the PAN, which ended one-party rule in Mexico by beating the PRI in 2000, but then lost power 12 years later.
While the PRI's big-tent approach to politics defies easy categories, Lopez Obrador has pitched himself as the most left-wing leader in Mexico since Lazaro Cardenas came to power in 1934, distributing land to peasant farmers and nationalizing foreign industry, including oil companies.
Like Cardenas, Lopez Obrador plans to help poor farmers, though he has stressed he will not expropriate private property.
Hammering home a message that he alone could end a "mafia of power" and root out corruption that marred outgoing President Enrique Pena Nieto's government, Lopez Obrador's star has risen amid the scandals and Mexico's descent into dizzying levels of violence, which at times draws comparisons with war zones.
Since former President Felipe Calderon of the PAN sent the army to fight cartels in 2007, some 230,000 people have been killed. Lopez Obrador says he will try new approaches to end the bloodshed, including a vaguely defined amnesty for some who work for drug gangs.
In contrast to the high-rolling ways of many PRI grandees, Lopez Obrador says he will live in his own middle-class home, turn the official residence into an arts center, sell the presidential plane and slash his salary.
He has been vague on policy details. Seeking to corral support from economic nationalists, leftist liberals and social conservatives, he has pledged to combat inequality, improve pay and welfare spending, and run a tight budget.
However, Lopez Obrador has brought onto his team people who many Mexicans associate with the same network of politicians and bosses he said he would finish off.
Such pragmatism, which included appointing a former cigarette baron as his chief of staff, is seen by some loyalists as a betrayal of his ideals, and his government will likely be subject to extensive scrutiny for signs of misconduct.
(Reporting by Christine Murray and Diego Oré Additional reporting by Dave Graham, Noe Torres, Daina Beth Solomon Writing by Frank Jack Daniel; Editing by Dave Graham, Jeffrey Benkoe, Paul Simao and Mike Collett-White)