With inflation hovering near levels not seen in 40 years, higher-income Americans turned to Walmart to cut costs on groceries while its lower-income customers swapped out deli meats for less expensive hot dogs and canned tuna.

That boosted sales for Walmart in the second quarter, the company reported Tuesday, but the downward shift at almost all points along the social spectrum cut into profit margins.

And the same forces shaping choices that Americans make about where they shop for food and what they eat are forcing Walmart to cut prices and clear huge inventories of things that customers clamored for during the pandemic; TVs, casual clothing, sporting goods and a host of other items not considered essential.

"We're pleased to see more customers choosing Walmart during this inflationary period," said CEO Doug McMillon. "The actions we've taken to improve inventory levels in the U.S., along with a heavier mix of sales in grocery put pressure on profit margin."

Retailers this year have had to stay on top of shopping habits vastly changed by both rising costs and consumers that would rather spend money outside of the home rather than on it as the pandemic eases.

The retailer beat Wall Street expectations and sales at stores open at least a year rose 6.5% as more Americans looked to trim spending in various ways through Walmart.

After being caught off guard by how quickly its customers pivoted, Walmart said that it's making progress getting rid of excess inventory, though it remains an issue. The company on Tuesday said the decline in profit that it forecast just a month ago will be smaller than it had feared.

Shares of Walmart jumped nearly 6% Tuesday.

Walmart Inc., based in Bentonville, Arkansas, is among the first major retailers to report quarterly results and is considered a crucial barometer of spending given its size and the breadth of its customer base.

Walmart shocked Wall Street three weeks ago when it lowered its profit outlook for the first time since 2015 and shares of a company that thrived during the pandemic tumbled 10%.

The warning followed an announcement by Target, another pandemic superstar, in the previous month that it was canceling orders from suppliers as its inventory piled up, untouched by consumers who no longer wanted to furnish their homes as COVID-19 eased its grip.

Sales of casual clothing, TVs and other electronics that flew off the shelves and into the homes of Americans sheltering in place went flat as people started going to restaurants, shows or traveling again.

Evidence of an enormous consumer shift registered in a number of places at Walmart. Sales of private grocery brands, which are typically less expensive, doubled from the first quarter to the second.

Walmart canceled billions of dollars worth of orders to equalize its inventory and now believes that only about 15% of its total inventory growth during the second quarter is above optimal levels.

The situation stabilized somewhat as the quarter wore on, Walmart said. It saw the influx of new customers, and lower gas prices in recent weeks offered some relief. Strong back-to-school spending helped.

Walmart Inc. earned $5.15 billion, or $1.88 per share, or $1.77 excluding one-time costs and charges. That easily beat the $1.62 per share that Wall Street was looking for, according to FactSet.

It also topped last year's profit of $4.27 billion.

Sales rose 8.4% to $152.86 billion, topping industry analyst projections.

It now expects consolidated adjusted operating income to decline 9% to 11% for the year, an improvement from the company's prior guidance of 11% to 13%.

Anne D'Innocenzio reports for the Associated Press.

Copyright 2022 AP. All rights reserved. This material may not be published, broadcast, rewritten, or redistributed.