-1

I know it's a pretty easy question, but I'm new in Python and need your help.

I have a list:

['232','43','1, 2, 3','5']

And I need to get:

['232','43','1','2','3','5']

That string with commas could be in any position in the list and I don't know how many such strings are present (the number of such strings may be changed). But in the string with commas there will always be exactly a single space after the comma, and there will never be a comma at the end of the string.

What is the easiest way to do that?

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
Max Feschenko
  • 76
  • 1
  • 4
  • 2
    Do you already know how to turn `'1, 2, 3'` into `['1', '2', '3']`? – Martijn Pieters Aug 28 '18 at 16:53
  • If you Google the phrase "Python tutorial string list", you’ll find tutorials that can explain it much better than we can in an answer here. – Prune Aug 28 '18 at 16:55
  • 1
    Also, tell us more about how your input structure works in *general terms*. Do you always have a string with commas in the 3rd position? In the last position? Or in any of the list positions, where there could be any number of such strings? If the latter, should all of those be expanded? – Martijn Pieters Aug 28 '18 at 16:55
  • Put differently: *A single input and output example* is not enough here without a clear explanation that shows that there are not more variations that we need to take into account. – Martijn Pieters Aug 28 '18 at 16:56
  • I can have a string with comas in any of the list positions and I don't know how much of them could be. I've just wrote a very basic example – Max Feschenko Aug 28 '18 at 16:57
  • For this example, say the list is named `foo`, `foo[:2] + foo[2].split(',')`. – tdelaney Aug 28 '18 at 16:58
  • 1
    `list(itertools.chain.from_iterable(map(lambda x:x.split(","),original_list)))` (just a heads up programming gets harder from here) – Joran Beasley Aug 28 '18 at 17:02
  • 1
    [x for y in ['232','43','1, 2, 3'] for x in y.split(', ')] Assuming that the string is a list always separated by comma and a space ", " – vin Aug 28 '18 at 17:04
  • In the string with commas will there always be _exactly_ a single space after the comma? Can it have a comma at the end, with no number following, like `'1, 2, 3, '` ? – PM 2Ring Aug 28 '18 at 17:08
  • 1
    Let's _not_ offer answers in comments, ok? If the OP clarifies, we can vote to re-open so that proper answers can be submitted. – PM 2Ring Aug 28 '18 at 17:09
  • @MartijnPieters - I think the edit is sufficient to take this off of hold. – tdelaney Aug 28 '18 at 17:10
  • @PM2Ring exactly a single space after the coma, just like this structure '1, 2, 3' – Max Feschenko Aug 28 '18 at 17:11
  • @tdelaney: now it is a duplicate, see [How can I replace a text delimited string list item with multiple list items in a python list?](//stackoverflow.com/q/45465661) – Martijn Pieters Aug 28 '18 at 17:15

1 Answers1

0

Assuming that the string is a list always separated by comma and a space ", "

arr = ['232','43','1, 2, 3','5']
[x for y in arr for x in y.split(', ')] 
vin
  • 960
  • 2
  • 14
  • 28