Timeline for How do I get the last element of a list?
Current License: CC BY-SA 3.0
7 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 14, 2018 at 5:32 | comment | added | Fr0zenFyr |
What will happen for my_test = [] last_item = my_test[len(my_test) - 1] . This will also throw an exception for IndexError like other answers. Although, I believe exceptions are valid and good because I know what to do with my preferred try:...except:...else:...finally:... pattern of coding in Python.
|
|
Nov 4, 2016 at 21:08 | comment | added | randomname12387955 |
Although it's not very pythonic I think this is in some respects better than the some_list[-1] approach because it's more logical, and shows what it is actually doing better than some_list[-1] in my opinion.
|
|
Dec 3, 2015 at 15:30 | comment | added | Matthew Purdon | @Pneumokok You make point, but I would argue that list indexing is a very basic Python technique compared to say generators. Also, why bother to use anything other than C or maybe javascript if you aren't going to take advantage of individual language tools and syntax? Then you can be consistently doing everything the hard way in all of your projects. | |
Oct 31, 2015 at 14:13 | comment | added | Radek Anuszewski | @MichaelWu There is no sense to do that. Pythonic way often is not self-explanatory, need more attention where you have to introduce new persons to project and, of course, wouldn't work when you have to switch to other languages like Java - you can't use Python specific knowledge. When you omit pythonic way as is possible, then also returning to project after months/years is much easier. | |
Oct 27, 2015 at 3:35 | comment | added | Michael Wu | since you're writing Python, you really should try to be more Pythonic | |
Oct 10, 2015 at 0:25 | comment | added | Dan Gayle | items[len(items) - 1] is essentially what Python is doing under the hood, but since the len of a sequence is already stored in the sequence there is no need to count it, you're creating more work than is necessary. | |
Oct 7, 2015 at 17:22 | history | answered | Radek Anuszewski | CC BY-SA 3.0 |