pythonic way to convert variable to list

Viewed 29001

I have a function whose input argument can either be an element or a list of elements. If this argument is a single element then I put it in a list so I can iterate over the input in a consistent manner.

Currently I have this:

def my_func(input):
    if not isinstance(input, list): input = [input]
    for e in input:
        ...

I am working with an existing API so I can't change the input parameters. Using isinstance() feels hacky, so is there a proper way to do this?

8 Answers
Related