GetLastElement([[2, 3], [2, [3, 4], [5, 6]]]) # Returns 6
GetLastElement([[3, [4]]]) # Returns 4
The problem is that I can't find an easy solution, and it affects readability. For example, if I wanted to get the last element, I would use [...][-1] instead of defining Last and using Last([...]) repeatedly—until I forget that I'm working on another project that doesn't contain Last.
I expect the answers to be non-recursive (meaning, they don't use recursion). Importing is fine for me.
This is what I except the GetLastElement function to be
def GetLastElement(List_to_get):
ToReturn = List_to_get
while True:
if isinstance(ToReturn, list):
ToReturn = ToReturn[-1]
else:
return ToReturn
However, I want to shorten it in one line of code without using def or lambda.
0 comments:
Post a Comment
Thanks