Hi I'm relatively new to python and not quite getting a resolve for the mentioned issue
I have an input list ['abc','def','asd,trp','faq']
Expected Output list ['abc','def','asd','trp','faq']
please help in achieving the same
You can iterate over the list and check if a comma exists and if it does, split and extend, if not, append to an output list.
lst = ['abc','def','asd,trp','faq']
out = []
for item in lst:
if ',' in item:
out.extend(item.split(','))
else:
out.append(item)
Output:
['abc', 'def', 'asd', 'trp', 'faq']
Since you tagged pandas, using pandas, you can also do:
out = pd.Series(lst).str.split(',').explode().tolist()