0
dics = []

The following shows the values in the list of dics:

["['Definition.", "', 'Exceptions.", "', 'Compelled Disclosure."]

But, what I am wondering is how I can remove the unnecessary values from the list such as:

  • [' for Definition.
  • ', ' for Exceptions.
  • ',' for Compelled Disclosure. I'd like to define the list to be like this:
["Definition.", "Exceptions.", "Compelled Disclosure."]

without unnecessary data. It would be really appreciated if you can help with any. Thanks.

Mario
  • 1,631
  • 2
  • 21
  • 51
Gideok Seong
  • 65
  • 3
  • 15
  • Use a regex and split your string? – Devesh Kumar Singh Jun 05 '19 at 18:43
  • 1
    The question you should be asking is how did your data get like this? You should fix the problem upstream. – pault Jun 05 '19 at 18:43
  • 1
    How did you get that list? If that was a string that you `.split`ted on whitespace, It might be easier to parse it properly in the first place. – Norrius Jun 05 '19 at 18:44
  • 1
    looks like a botched case of json conversion gone really wrong. – Paritosh Singh Jun 05 '19 at 18:44
  • 1
    [How to turn a string representation of a list into a list](https://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list). – pault Jun 05 '19 at 18:45
  • List comprehension with any suggestions from [this answer](https://stackoverflow.com/a/3939381/6779606) would work well. – Stephen B Jun 05 '19 at 18:45
  • Hacky fix would be to join this back together as a string and then use `literal_eval`: `literal_eval("".join(["['Definition.", "', 'Exceptions.", "', 'Compelled Disclosure."]) + "']")` – pault Jun 05 '19 at 18:47

2 Answers2

2
v = ["['Definition.", "', 'Exceptions.", "', 'Compelled Disclosure."]
s = "|".join(v)
undesired = "[,' "

for u in undesired:
    s = s.replace(u, "")

print(s.split("|"))  
user508402
  • 496
  • 1
  • 4
  • 19
  • Thank you for the help. By the way, for "Compelled Disclosure" it shows like this CompelledDisclosure, without the space. – Gideok Seong Jun 05 '19 at 18:49
  • Removing the space from undesired and run all resulting elements throught strip() instead will take care of that – user508402 Jun 05 '19 at 19:02
0

You can do it using a combination of filter and isalpha method to rule out every character of each word that is not a letter:

dics = ["['Definition.", "', 'Exceptions.", "', 'Compelled Disclosure."]
dics = [(''.join(filter(lambda letter: letter.isalpha() or letter==' ',elem))+elem[-1]).strip() for elem in dics]
print(dics)

Output:

['Definition.', 'Exceptions.', 'Compelled Disclosure.']
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29