What do you mean by "lost"? You mean the nation building part? Because Afghanistan and Iraq didn't win those wars. The occupation and attempts at nation building are a different story.
When I think of winning wars, I think of one military defeating another, and the result being that the defeated government surrenders to the other, or is unable to continue waging war. I consider nation building to be separate from that, even though it involves military occupation. One can win the initial war, and lose the occupation, or at least fail to achieve stated objectives.
I don't think anyone has any illusions that the US lost militarily, but it's a bit disingenuous to only look at that part of the picture. The majority of expenses were on the occupation and nation-building efforts, which are a mixed bag as far as success is concerned.
Some examples of the "good" are the massive amount of infrastructure that was built/re-built and the trade that was established with other countries.
Don't mistake what I'm saying for a defense of the invasion of either country. Just trying to look at things objectively rather than through an emotional lens.