Here is a simple function to do what you want:
def get_length_or_value(l, *i):
item = l
for index in i:
item = item[index]
return len(item) if isinstance(item, list) else item
This function first finds the element at the given indices (if any), and then returns the appropriate value based off wether the element is a list
type or not.
You can call it like this:get_length_or_value(lol)
get_length_or_value(lol, 0)
get_length_or_value(lol, 0, 1)
get_length_or_value(lol, 0, 1, 2)
get_length_or_value(lol, 1, 0, 2)
Those calls all return the expected values you posted above.
1
solved indexing for python list of lists