Extracting the a value from a tuple when the other values are unused

前端 未结 6 1353
一整个雨季
一整个雨季 2021-02-04 10:42

I have a tuple foo which contains something I don\'t care about and something I do.

foo = (something_i_dont_need, something_i_need)
<
6条回答
  •  萌比男神i
    2021-02-04 11:06

    I think the usual way of doing it

    x=foo[index]
    

    Using _ is less common, and I think also discouraged. Using _ is also unwieldy when you need only a few elements out of a long tuple/list. Slicing also comes handy when you are only choosing a contiguous subsequence.

    But at the end of the day I think it is just a matter of subjective preference. Use whatever that looks more readable to you and your team.

提交回复
热议问题