user2243831
user2243831

Reputation: 47

Python merge single nested lists

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]

Upvotes: 2

Views: 58

Answers (1)

thefourtheye
thefourtheye

Reputation: 239573

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

Upvotes: 4

Related Questions