Gandhi learned Tamil during his imprisonment in South Africa, specifically from Tamil prisoners he befriended. He did this to better communicate with the Tamil-speaking community in South Africa. The Indian jails mentioned (Rajahmundry, Yeravada, Tihar) are where he was imprisoned during India's freedom movement, but he learned Tamil earlier in South Africa.