I'm referring to this question, and especially the comments to the first answer from @David Robinson and @mgilson: Sum the second value of each tuple in a list
The original question was to sum the second value of each tuble:
structure = [('a', 1), ('b', 3), ('c', 2)]
First Answer:
sum(n for _, n in structure)
Second Answer:
sum(x[1] for x in structure)
According to discussion, the first answer is 50% faster.
Once I figured out what the first answer does (coming from Perl, I Googled for the special _ variable means in python), I got wondering how come what appears as a pure subset task (getting only the second element of each tuple vs. getting and binding into variables both elements) is actually slower? Is it a missing opportunity to optimize index access in Python? Am I missing something the second answer does which takes time?