I have a list of values which are of different types and I would like to check if a substring exists in the given list and when found, I want to get back the index of it.
I tried doing it with any() but it throws me an error saying in <string>' requires string as left operand, not int. I am assuming, all the values in the list needs to be string in order to use any().
What other way I could use to achieve this?
Here is my Current code:
list_val = [1,18.0, 'printer', 'EXTRACT (123)']
string_val = 'EXTRACT'
any(x in string_val for x in list_val)
I even tried converting all the non-strings into strings and then wanted to use the above any() logic, but that was giving me other errors. I tried the following:
for i, val in enumerate(list_val):
if not isinstance(val, str):
list_val[i] = str(val)
This throws me the error: isinstance() arg 2 must be a type or tuple of types
string_val in x, to begin with.strtype. Tryprint(str)to check. If the output is not<type 'str'>, that's a problem.str. Get rid of it withdel str.