A simple approach could be:
def double(li):
try:
return [double(x) for x in li]
except:
return 2*li
Note, however, that this solution "enumerates" all kinds of iterations. A more complex version, which supports the types of your iterations, can process any nested structure of lists, tuples, sets, etc., Will have this line in the try block:
return type(li)(map(double, li))
This creates and returns an object of the lioriginal type (list, tuple, etc.) with a list (Py2) or a map object (Py3) of all doubled elements in li.
source
share