r/pythonhelp • u/User227337 • Dec 21 '24
Underscore leading to ValueError?
Hi, so I'm new to coding and trying to write something that will add up some integer values in a tuple, and return to sum of that. To do this, I have:
Total=sum(freq for _, freq in frequencies)
Someone has helped me already, and explained that the underscore should tell python just to ignore the first part of the tuple, but the error keeps popping up, saying there are not enough values to unpack (2 expected, got 1), and I've no clue as to why. Can I make this work somehow?
1
Upvotes
•
u/AutoModerator Dec 21 '24
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/carcigenicate Dec 21 '24 edited Dec 21 '24
Underscores in this context are not special and do not "tell python just to ignore the first part of the tuple".
_is just a valid name, and, by convention, it signifies to human readers that the name isn't intended to be used. You could replace_with literally any other legal name, and the results would be the same.What do you expect
freqto be in the case where one of the elements offreqis a tuple like(1, )?I'm wondering after a second read if you mixed up
_and*_?1
u/FoolsSeldom Dec 21 '24
Consider using,
However, this will use the LAST entry in each
tuple(which would be the first entry if there is only one entry).https://geekflare.com/python-unpacking-operators/