2

I have a variable that holds lists

 s=[16, 29, 16] 
   [16, 16, 16]

I want to combine them like this

 combined = [16,16]
            [29,16]
            [16,16]
1
  • 1
    The world is irrational. Some (similar) questions get downvoted and some upvoted! Commented Mar 23, 2014 at 7:26

1 Answer 1

4

Use zip function, like this

s = [[16, 29, 16], [16, 16, 16]]
print zip(*s)
# [(16, 16), (29, 16), (16, 16)]

If you want the output to be a list of lists, then you can simply do

print map(list, zip(*s))
# [[16, 16], [29, 16], [16, 16]]
Sign up to request clarification or add additional context in comments.

7 Comments

@ i am getting error like this Traceback (most recent call last): File "2.py", line 42, in <module> print zip(*score) TypeError: zip argument #1 must support iteration @thefourtheye
@user2243831 Please edit the question and show how score is defined
sorry i mentioned you wrong!i am looping a file through while loop and lists are created for every line in file @thefourtheye
@user2243831 mmm, I am going to say "Okay, So?"
@user2243831 Please be specific when you ask questions. Edit your question to include all the relevant details to the question, sample input and expected output. Otherwise we both would be wasting our time.
|

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.