0

I have the following data structure:

data = (['test1','test2','test3'], ['foo1','foo2','foo3'], ['bar1','bar2','bar3'])

I want to iterate through this data structure and create a new tuple which appends position 1 of each list to it. I would like to create a data structure with

(test1,foo1,bar1), (test2,foo2,bar2), (test3,foo3,bar3)
AlG
  • 14,697
  • 4
  • 41
  • 54
Ciaran
  • 1,139
  • 2
  • 11
  • 14

2 Answers2

3

This is an easy zip with argument unpacking:

print zip(*data)

e.g.:

>>> data = (['test1','test2','test3'],['foo1','foo2','foo3'],['bar1','bar2','bar3'])
>>> zip(*data)
[('test1', 'foo1', 'bar1'), ('test2', 'foo2', 'bar2'), ('test3', 'foo3', 'bar3')]
mgilson
  • 300,191
  • 65
  • 633
  • 696
3

Unzip it via zip():

>>> data = (['test1','test2','test3'],['foo1','foo2','foo3'],['bar1','bar2','bar3'])
>>> zip(*data)
[('test1', 'foo1', 'bar1'), ('test2', 'foo2', 'bar2'), ('test3', 'foo3', 'bar3')]

Also see: Unzipping and the * operator.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195