0

I am trying to get rid of hyphens in words in a list through the below code


listA=['End-to-End Encryption']

listmain= '-'.join(listA).split('-')

The output I get is

['End', '', '', 'to', '', '', 'End', 'Encryption']

How do I get rid of unnecessary blank elements created

Ideal output required

['End', 'to', 'End', 'Encryption']

How to achieve this.

venkatttaknev
  • 669
  • 1
  • 7
  • 21

3 Answers3

1

Split it by multiple separators. By regex.

listA = ['End-to-End Encryption']
listmain = re.split(r'\s|-', listA[0])
Raymond Reddington
  • 1,709
  • 1
  • 13
  • 21
0

This should do the job and is dynamic for more than 1 sent in listA

listmain = [val.replace("-"," ").split() for val in listA]

Eg:

listA = ["End-to-End Encryption", "End-to-End Decryption"]
listmain = [val.replace("-"," ").split() for val in listA]

O/P:

[["End", "to" ,"End", "Encryption"],["End","to","End","Decryption"]]
Faizan Naseer
  • 589
  • 3
  • 12
0

The following code works flawlessly:

listA=['End-to-End Encryption']
for el in listA:
    str2 = el.split('-')
    print(str2)

The Output of this code is:

['End', 'to', 'End Encryption']
Code_Ninja
  • 1,729
  • 1
  • 14
  • 38