0

If I have a list formed by the command dic.keys() like this:

[
  "['my', 'modem']", "['technical', 'schematics']", "['still', 'glad']",
  "['spent', 'calling']", "['most', 'feared']", "['the', 'sysop']",
  "['s', 'mystique']", "['had', 'been']", "['of', 'doom']",
  "['i', 'hooked']", "['in', 'california']", "['my', 'ego']",
  "['until', 'my']", "['didn', 't']"
]

The keys are formed by str(),so it looks like list, but is a string. My question is, how can you separate each item to make it look like this?

[ 'my', 'modem', 'techenical', ... ]
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
LOPWQ
  • 23
  • 4
  • Possible duplicate of [Flattening a shallow list in Python](http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python) – Tomasz Jakub Rup Nov 15 '15 at 19:53

1 Answers1

0

Just do a pattern matching for each string:

res = []
for item in l:
    m = re.match("\['(.*)', '(.*)'\]", item)
    res.append(m.group(1))
    res.append(m.group(2))
#>>> res - ['my', 'modem', 'technical', ...]
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176