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
First, there is no general method that could tell a "single element" from "list of elements" since by definition list can be an element of another list.
I would say you need to define what kinds of data you might have, so that you might have:
list against anything else
isinstance(input, list) (so your example is correct)basestring in Python 2.x, str in Python 3.x)
isinstance(myvar, collections.Sequence) and not isinstance(myvar, str)int, str, MyClass
isinstance(input, (int, str, MyClass)).
try:
input = iter(input) if not isinstance(input, str) else [input]
except TypeError:
input = [input]