Is there a straightforward way to get the index of an item I just appended to a list? I need to keep track of the last added item.
I came up with two possible solutions:
# Workaround 1
# The last added is the one at index len(li) - 1
>> li = ['a', 'b', 'c',]
>> li.append('d')
>> last_index = len(li) - 1
>> last_item = li[len(li) - 1]
# Workaround 2
# Use of insert at index 0 so I know index of last added
>> li = ['a', 'b', 'c',]
>> li.insert(0, 'd')
>> last_item = li[0]
Is there a trick to get the index of an appended item?
If there's not, which of the above would you use and why? Any different workaround you suggest?