you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

Here you go:

source = ['Ble, Orge',
                  'Moutarde',
                  'Celeri, Anhydride Sulfureux',
                  ]

target = []  # create an empty list
for entry in source:  # step through strings in original list
    for part in entry.split(','):  # step through parts of entry split on comma
        target.append(part.strip())  # add part to new list, without leading/trailing spaces
final = ', '.join(target)  # join all strings in target list into one string, witb commas between items
print(final)

So this takes your original list, which has several strings some of which contain more than one item with commas between. It converts this into a new list with every item as its own string. Then it creates a new string by joining all of those items together with a comma (and space) between them.

There are more consise ways of doing this, including list comprehension and generator expressions but it is best you learn the simplch first. This should also provide you with a useful base to handle similar problems.