-3

I have a list that looks like:

test = ['bla bla','affect /affected / affecting']
print(test)

and I would like to create new elements in the list by separating the ones that are one element but separated through the "/" symbol.

E.g. desired output in this case:

test_new = ['bla bla','affect','affected','affecting']

How can I do that?

EDIT: The list can have multiple elements, and not all have the "/" symbol

adrCoder
  • 3,145
  • 4
  • 31
  • 56
  • Is `test` always a list with exactly one item in it…? – deceze Mar 23 '20 at 09:36
  • *"....by separating the ones that are **one element** but..."*. It would be nice to post an example that contains elements that are not *one element* to demonstrate what you want to do in the most general of cases. – Ma0 Mar 23 '20 at 09:38
  • No the list can have multiple items – adrCoder Mar 23 '20 at 09:43

2 Answers2

4

You can use a list comprehension and split on '/' and strip to remove spaces (assuming you have multiple strings, being a list):

[j.strip() for i in test for j in i.split('/')]
# ['affect', 'affected', 'affecting']
yatu
  • 86,083
  • 12
  • 84
  • 139
0
testEntry=test[0]
testEntry=test.replace(' ','')
test=testEntry.split('/')
mark pedersen
  • 245
  • 1
  • 9
  • 1
    I want to do it on the whole list not only one element. Some elements may not contain "/" – adrCoder Mar 23 '20 at 09:50
  • 3
    Please do not post only code as an answer, but also include an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality and are more likely to attract upvotes. – Suraj Kumar Mar 24 '20 at 06:43