Dec-09-2019, 10:12 PM
Don't worry about the one true way to do things. That is probably the most often broken part of the Zen of Python.
I don't think either [4:] or [-4:] is more Pythonic. Here's how I would think of it. The colon is to the right, so it's printing everything to the right of that index. The first one prints everything after the fourth item, and the second one prints the last four items (because of the negative). If your intent is to get 'the last four digits', then I think [-4:] is clearer. More important than that is what if you get a 9 item list? or a 7 item list. In either of those cases, [-4:] will give you the correct number of items, but [4:] may not give you the right number of items.
I don't think either [4:] or [-4:] is more Pythonic. Here's how I would think of it. The colon is to the right, so it's printing everything to the right of that index. The first one prints everything after the fourth item, and the second one prints the last four items (because of the negative). If your intent is to get 'the last four digits', then I think [-4:] is clearer. More important than that is what if you get a 9 item list? or a 7 item list. In either of those cases, [-4:] will give you the correct number of items, but [4:] may not give you the right number of items.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures