I find with these things it can be important to get the result but also know where the result came from. So, all as well as returning the data, it tells where the index it was found at in the input list.
Also takes into account Several occurences of -separated items, as well as something like foo-bar-zoom.
def extractor(li : list[str]) -> list[tuple[str,int]]:
res = []
for ix, v in enumerate(li):
if "-" in v:
found = v.split('-',maxsplit=1)[1]
res.append((found,ix))
return res
Some runs:
✅!
extractor for ['BIA-660', 'Web', 'Analytics']
exp :[('660', 0)]:
got :[('660', 0)]:
✅!
extractor for ['BIA-660', 'Web-3.0', 'Analytics']
exp :[('660', 0), ('3.0', 1)]:
got :[('660', 0), ('3.0', 1)]:
✅!
extractor for ['BIA-660', 'foo-bar-zoom']
exp :[('660', 0), ('bar-zoom', 1)]:
got :[('660', 0), ('bar-zoom', 1)]:
You could extend it to ignore list items that are not strings (isinstance(v,str)) and extracted values that are not numbers (check that float(found) succeeds).