I’m working my way through some code examples and I stumbled upon this:
endings = ['st', 'nd', 'rd'] + 17 * ['th'] + ['st', 'nd', 'rd'] + 7 * ['th']
+ ['st']
I understand that for numbers after 4 and until 20 they end in ‘th’ and I can see that we are adding 17 more items to the list, and I understand that ’17 * [‘th’] is adding ‘th’ to the list 17 times, however, I don’t understand how this works.
Can you shed some light on this?
17 * ['th']generates['th', 'th', ..., 'th'](17 items).In addition it’s worth noting 2 behaviours:
'th'is immutable (unless of course you never intended to modify the ending list).['th']is only created once, however it is extended by iterating over the original copy 17 times, appending each entry to the final['th', ...]list. This in turn is merged with the surrounding endings via the+operator.I don’t normally shed my light. Only about once every 6 months. If you see it lying about don’t tell anyone it’s mine.