How to convert list of tuples from Python to most pythonic (possibly with zip)

I have this data structure:

[((u'die', u'die'), (u'welt', u'WELT')), 
 ((u'welt', u'WELT'), (u'ist', u'ist'))]

how can i convert the above structure to the most pyphonic? With zip?

[((u'die', u'welt'), (u'die', u'WELT')), 
 ((u'welt', u'ist'), (u'WELT', u'ist'))]
+4
source share
1 answer

You can use the function zip()and list comprehension for this , since -

>>> lst = [((u'die', u'die'), (u'welt', u'WELT')),
...  ((u'welt', u'WELT'), (u'ist', u'ist'))]
>>>
>>> [tuple(zip(*x)) for x in lst]
[(('die', 'welt'), ('die', 'WELT')), (('welt', 'ist'), ('WELT', 'ist'))]
+6
source

Source: https://habr.com/ru/post/1606911/


All Articles