1

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'].

7 Answers 7

3

you could use split and join to get from one representation to the other:

print('group1, group2, group3'.split(', '))
# ['group1', 'group2', 'group3']
print(', '.join(['group1', 'group2', 'group3']))
# group1, group2, group3

in a function you could do this:

def f(items, index, separator=', '):
    if isinstance(items, str):
        items = items.split(separator)
    return items[index]


print(f('group1, group2, group3', 1))        # group2
print(f(['group1', 'group2', 'group3'], 1))  # group2
Sign up to request clarification or add additional context in comments.

2 Comments

can a separator be conditional? ', ' or ','. I guess a regex is more appropriate?
sure, if you need to split several things re.split should work.
1

You were almost there in your edit:

def f(lst):
    return [t.strip() for t in lst[0].split(',')] if len(lst) == 1 else lst

>>> a = ['group1', 'group2', 'group3','group4']
>>> b = ['group1,group2, group3,  group4']

>>> f(a)
['group1', 'group2', 'group3', 'group4']
>>> f(b)
['group1', 'group2', 'group3', 'group4']

Comments

1

How about using split():

Code:

['group1, group2, group3'][0].split(', ')

Test Code:

x = ['group1, group2, group3']    
print(x[0].split(', '))

Results:

['group1', 'group2', 'group3']

To return:

To return use str.join()

y = x[0].split(', ')
print(y)

xx = [', '.join(y)]
print(xx)

Results:

['group1', 'group2', 'group3']
['group1, group2, group3']

3 Comments

Thanks. Is there a way to make the delimiter space insensitive: 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?
@SimeonLeyzerzon You can split on the bare comma and strip the tokens: y = [t.strip() for t in x[0].split(',').
@schwobaseggl Your comment seems to be the closest to what I'm after - you may want to make it into an official answer, I've updated my question with a bit more clarification if you care to take another look. Thanks.
1

You can define a function using str.split on , followed by flattening of result using itertools.chain

>>> from itertools import chain
>>> f = lambda x: list(chain(*[a.split(', ') for a in x]))

>>> f(['group1', 'group2', 'group3'])
>>> ['group1', 'group2', 'group3']

>>> f(['group1, group2, group3'])
>>> ['group1', 'group2', 'group3']

In case of variable separator and optional space, regex would be a better choice

>>> import re
>>> f = lambda x, sep: list(chain(*[re.split('{}\s*'.format(sep), a) for a in x]))
>>> f(['group1, group2,group3'],',')
>>> ['group1', 'group2', 'group3']

Comments

0

split() and join() can try .

case1 = ['group1, group2, group3']
output = [ele.split(",") for ele in case1]

Output for case-1

[['group1', ' group2', ' group3']]

Case-2

case2 = [['group1', ' group2', ' group3']]
output = [", ".join(ele) for ele in case2]

Output for case-2

['group1, group2, group3']

Comments

0

By using str.replace() to eliminate all white spaces you can use a formula that will work on all cases presented.

lst = ['group1, group2, group3']

res = lst[0].replace(' ', '').split(',')
# ['group1', 'group2', 'group3']

Comments

0

I think it's most clear simply using split and strip.

# various scenarios to test
TESTS = [
    ['group1, group2, group3'],
    ['group1', 'group2', 'group3'],
    ['group1,group2,  group3'],
    ['group1', 'group2, group3'],
    ['group1 ,group2', 'group3'],
]


def normalize(args, delimiter=','):
    results = []
    for arg in args:
        results.extend(x.strip() for x in arg.split(delimiter))
    return results


for args in TESTS:
    assert normalize(args) == ['group1', 'group2', 'group3']


# test alternative delimiter
assert normalize(['group1 | group2 | group3'], delimiter='|') == ['group1', 'group2', 'group3']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.