The area where Mexico City was built was a lakebed, not a swamp. The city was built on a series of islands in Lake Texcoco, which was a large, shallow lake that covered much of the Valley of Mexico. The lake was fed by several rivers and springs, and it was a source of water for the people who lived in the valley.
The Aztecs, who founded Mexico City in the 14th century, built the city on a series of artificial islands that they created by dredging up mud from the lakebed. The islands were held together by wooden pilings, and they were covered with a layer of soil. The Aztecs built canals to connect the islands, and they used these canals to transport goods and people.
The Spanish conquistadors who arrived in Mexico in the 16th century decided to keep the city in the same location. They drained Lake Texcoco and built the city on the drained lakebed. The city has continued to grow and expand over the centuries, and it is now one of the largest cities in the world.