Guided Project 2: Exploring Hacker News post (Confused with FOR loop when printing hours with highest average comments)

Hello, I hope you are having a nice winter break!

I got confused by FOR loops in the the guided project 2. In the last step where we have to print the hours with the highest average of comments in a proper format, we write the following code:

# Sort the values and print the the 5 hours with the highest average comments.

print("Top 5 Hours for 'Ask HN' Comments")
for avg, hr in sorted_swap[:5]:
    print(
        "{}: {:.2f} average comments per post".format(
            dt.datetime.strptime(hr, "%H").strftime("%H:%M"),avg
        )
    )

But I was wondering, how does python know that “avg” and “hr” included in FOR loop, are the actual average and hours that we included previously in the sorted swap list of lists? We could have used “a” and “b” instead of "avg and “hr” as well right? But then how will python know where to put actual average and hours in “{}: {:.2f} average comments per post” ? I guess I am conceptually confused with the FOR loop when we give 2 inputs (“avg” and “hr”) instead of 1 (for example, “for row in sorted swap”.

Will this example help to understand how it all works?

>>> my_list = ['A', 'B', 'C']
>>> x, y, z = my_list
>>> print("First is {}, second is {}, third is {}".format(x, y, z))
First is A, second is B, third is C
2 Likes