He notched 12 goals in 50 games in Claret and Blue, including a double in the dramatic 3-2 Carabao Cup victory at Tottenham Hotspur this season.
Ayew, who spent the 2015/16 season at Liberty Stadium prior to signing for the Hammers, will link up with his brother Jordan at the South Wales club. The club website says “Everyone at West Ham United wishes Andre a successful future with Swansea City.”
Ayew belongs the second player to leave West Ham in as many days after Sakho left for Rennes for a transfer fee of £3.7m plus £2.4m in add-ons.