I've got a variable that could either be a string or a tuple (I don't know ahead of time) and I need to work with it as a list.
Essentially, I want to transform the following into a list comprehension.
variable = 'id'
final = []
if isinstance(variable, str):
final.append(variable)
elif isinstance(variable, tuple):
final = list(variable)
I was thinking something along the lines of the following (which gives me a syntax error).
final = [var for var in variable if isinstance(variable, tuple) else variable]
I've seen this question but it's not the same because the asker could use the for
loop at the end; mine only applies if it's a tuple.
NOTE: I would like the list comprehension to work if I use isinstance(variable, list)
as well as the tuple
one.