I want to iterate over a variable which can either be a list or a string. Problem is, I don't want to consider the string as a list of characters:
[1, 2, 3] => [1, 2, 3]
["1", "2"] => ["1", "2"]
"123" => ["123"] # instead of ["1", "2", "3"]
for x in foo
works for lists but iterates over characters assuming that foo is a string.
I know this can be done by checking the type (e.g. for x in (foo if type(foo) is list else [foo])
) but I get a feeling there must be a better way ...