Jabb
Jabb

Reputation: 3502

How to transform a list of tuples with Python in the most pythonic way (perhaps 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 transform the above to the below structure in the most pythonic way? With zip?

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

Upvotes: 4

Views: 59

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90889

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

>>> 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'))]

Upvotes: 6

Related Questions