In Python, what would be the most elegant way of converting this structure:
['group1, group2, group3']
into this structure:
['group1', 'group2', 'group3']
and potentially back.
What is needed is a function that can take either a list of a string of comma separated values (first case) or a list of strings of the same values (second case) and handle them as one and the same: a list of strings.
In pseudocode:
x = ['group1, group2, group3']
y = ['group1', 'group2', 'group3']
f(x) <==> f(y) <- equivalent behavior
Also, if using split() as per suggestions:
Is there a way to make the delimiter space insensitive or conditional or a regex: I'd like to get to the ['group1', 'group2', 'group3'] result in either ['group1, group2, group3'] or ['group1,group2,group3'] or even this ['group1, group2,group3'] (or a combination thereof) as an input?
A bit more clarification:
>>> single_string = False
>>> a = ['group1', 'group2', 'group3','group4']
>>> [t.strip() for t in [a][0].split(',')] if single_string else a
['group1', 'group2', 'group3', 'group4']
>>> single_string = True
>>> b = ['group1,group2, group3, group4']
>>> [t.strip() for t in [b][0].split(',')] if single_string else b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'split'
>>>
Basically, I'm looking for the most elegant Python conditional expression that would result in the same output both in case of a and b above:['group1', 'group2', 'group3', 'group4'].