0

I am forced to use comma separation in one of my input arguments to separate multiple values. So I end up with

my_string = ['a,b,c']

How can I convert this so that

my_new_string = ['a', 'b', 'c']
Jepper
  • 1,092
  • 3
  • 11
  • 24
  • `my_string` is a `list`, is there any scenario where it should hold more than one string? – Szabolcs Mar 05 '18 at 10:14
  • 1
    Possible duplicate of [How to convert comma-delimited string to list in Python?](https://stackoverflow.com/questions/7844118/how-to-convert-comma-delimited-string-to-list-in-python) – Artier Mar 05 '18 at 10:19
  • Why this `argparse` tag ? Can you explicit what's your use case ? It may helps people to provide better answer :) – NiziL Mar 05 '18 at 10:19
  • It's not a duplicate question, since it's a string contained in a list... Anyway, apologies to all for calling it my_string - I should have called it my_list...! – Jepper Mar 05 '18 at 10:54
  • @NiziL the use case is that the script is called from Jenkins, using Jenkins shell implemention. It's challenging to call the python script with a single space delimited argument without Jenkins injecting quotes, thus the second item in the space delimited argument is being interpreted as a new argument. Comma separating the input arg thus means Jenkins argument parsing does not interfere. – Jepper Mar 05 '18 at 10:59

3 Answers3

6

One possible way:

my_new_string = my_string[0].split(',')
Szabolcs
  • 3,990
  • 18
  • 38
-1

Try this one-liner:

my_new_string = [x for y in my_string for x in y.split(',')]
Yuankun
  • 6,875
  • 3
  • 32
  • 34
  • given that this question is tagged with `argparse` I don't think that this is the behavior OP is looking for. – Ma0 Mar 05 '18 at 10:17
-1

Try this one-liner:

print(list(my_string[0].split(',')))
DD.amor
  • 45
  • 3