On 24/07/18 06:41, Sayth Renshaw wrote:
On Tuesday, 24 July 2018 14:25:48 UTC+10, Rick Johnson  wrote:
Sayth Renshaw wrote:

elements = [['[{0}]'.format(element) for element in elements]for elements in 
data]

I would suggest you avoid list comprehensions until you master long-form loops.

I actually have the answer except for a glitch where on list element is an int.

My code

for item in data:
     out = '[{0}]'.format("][".join(item))
     print(out)

which prints out

[glossary]
[glossary][title]
[glossary][GlossDiv]
[glossary][GlossDiv][title]
[glossary][GlossDiv][GlossList]
....

However, in my source I have two lines like this

  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 
'GlossSeeAlso', 0],
  ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 
'GlossSeeAlso', 1],

when it hits these lines I get

TypeError: sequence item 6: expected str instance, int found

Do I need to do an explicit check for these 2 cases or is there a simpler way?

Cheers

Sayth


out = '[{0}]'.format("][".join(str(item)))

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to