Instead of constantly checking for permission with conditional statements, it's generally better to just iterate and handle exceptions when necessary using a try-except
block.
It's important to note, as others have pointed out, that creating different cases based on whether X
is a list of size 1
can be unnecessary since even a single-element list is iterable. When using the zip
function, it will stop at the shortest iterable:
>>> l = [1, 2, 3]
>>> y = [1, 2]
>>> list(zip(l, y))
[(1, 1), (2, 2)]
If you need to work with all values from the longer iterable while providing defaults for missing values in the shorter one, consider using zip_longest
from itertools along with an appropriate fillvalue
:
>>> from itertools import zip_longest
>>> list(zip_longest(l, y, fillvalue=0))
[(1, 1), (2, 2), (3, 0)]