If I have variables x
and y
, such that:
x
is always a stringy
can either be a string or a list of stringsHow can I create a list z == [x, <all elements of y>]
?
For instance:
x = 'x'
y = 'y'
# create z
assert z == ['x', 'y']
x = 'x'
y = ['y', 'y2']
# create z
assert z == ['x', 'y', 'y2']
z = [x] + (y if isinstance(y, list) else [y])
Generally I'd avoid having a y
that could be either a string or a list, though: it seems unnecessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With