LOS ANGELES — Gavin Lux singled in the go-ahead run with two outs in the seventh inning and the Los Angeles Dodgers rallied after blowing a 7-3 lead to beat the Chicago Cubs 10-8 on Wednesday night and avoid a series sweep.

Shohei Ohtani hit his 47th home run and stole his 48th base while three other Dodgers went deep in the first inning for the NL West leaders, whose magic number to clinch the division dropped to 11 after San Diego lost at Seattle.

The Dodgers walked 10 batters, including four by starter Bobby Miller.

“It was frustrating again, but it’s baseball. Not everything is going to be clicking at the same time,” third baseman Max Muncy said. “We had guys battling up there and it was good to see everyone not giving up.”

Switch hitting Tommy Edman homered for the fourth time in 24 hours. His two-out, two-run shot off Trey Wingenter in the eighth extended the Dodgers’ lead to 10-7. He also went deep in the first from the right side and later from the left, the first time he’s homered from both sides in the same game.

“Tommy is otherworldly these days,” Dodgers manager Dave Roberts said.

Trailing by three, the Cubs threatened in the ninth. Michael Kopech loaded the bases on consecutive walks to Dansby Swanson, Seiya Suzuki and former Dodger Cody Bellinger.

Kopech was called for a pitch-clock violation, giving an automatic ball to Isaac Paredes, whose sacrifice fly cut Chicago’s deficit to 10-8. Suzuki was thrown out trying to steal third and former Dodger Michael Busch struck out swinging to end the game as Kopech eked out his 13th save.

Alex Vesia (3-4) got the win with one inning of relief.