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)
4
  • 1
    These are not valid data structures, you are missing commas. Commented Mar 17, 2014 at 18:01
  • 1
    easy peasy, this will be answered in a zip Commented Mar 17, 2014 at 18:02
  • I just wrote it to outline what it looks like...My apologies Commented Mar 17, 2014 at 18:02
  • possible duplicate of Matrix Transpose in Python Commented Mar 17, 2014 at 18:04

2 Answers 2

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')]
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.