5 ms·
return [key, value]
by RogerL 2y ago
return [key, value]
- ghxst 2y agoWhy would you do this over `return key, value` which produces a tuple? Just curious.
- dgan 2y agoNot the parent, but i return heterogeneous lists of the same length to the excel to be used by xlwings. The first row being the headers, but every row below is obviously heterogeneous
- 0xDEADFED5 2y agojavascript refugee?
- slightwinder 2y agoTo quote the Zen of Python: Explicit is better than implicit. Readability counts.
- ErikBjare 2y agoHow does that apply in this case?
- slightwinder 2y ago"return key, value" is implicit. While "return [key, value]" is explicitly telling the full return-value. And it's (for me) more readable than "return (key, value)".
- Nab443 2y agoI don't see how "return (key, value)" is less readable than it's list counterpart. Now, why using a list that can grow, and so takes more space and maybe less efficient, for something that should not?
- slightwinder 2y ago"return (key, value)" can be read as a function-call. Especially as space after the function-name is allowed in python. And performance on that level is no serious topic in python anyway. But this is mainly just a personal preference.
- ghxst 2y agoA list and tuple are not the same thing. There could be reasons to require a list explicitly but I couldn't really think of any obvious ones which is why I asked.
- Myrmornis 2y agoYou should use a tuple there: it's a collection of fixed size where each slot has an identity. (There's a common confusion in Python circles that the main point of tuples is immutability; that's not so).