Hack Forums
[Python] Last position of an object in list - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Last position of an object in list (/showthread.php?tid=6168657)



Last position of an object in list - simmer - Aug 18, 2021

How can I fetch the last position of an item in a list?
index() references the first position…


RE: Last position of an object in list - vorticalbox - Aug 19, 2021

PHP Code

<?php 
my_list 
= [{},{},{}]

last = my_list[-1] 



RE: Last position of an object in list - SIM-888979 - Aug 19, 2021

Code
len(yourList) - 1

Pretty sure this works.


(Aug 19, 2021 02:23 AM)vorticalbox Wrote:  
PHP Code

<?php 
my_list 
= [{},{},{}]

last = my_list[-1] 

This returns the last item, not the index.


RE: Last position of an object in list - vorticalbox - Aug 19, 2021

(Aug 19, 2021 02:28 AM)Lotus Wrote:  
Code
len(yourList) - 1

Pretty sure this works.



This returns the last item, not the index.

very good point I misread the OP