Peaser
Peaser

Reputation: 575

zipping a list of lists according to index?

How can I use zip to zip a list of lists according to index?

a = [i for i in "four"]
b = [i for i in "help"]
c = [i for i in "stak"]
k = [a,b,c]

print zip(a,b,c)
print zip(k)

zip(a,b,c) prints out [('f', 'h', 's'), ('o', 'e', 't'), ('u', 'l', 'a'), ('r', 'p', 'k')]

This is what I need.

However zip(k) prints out [(['f', 'o', 'u', 'r'],), (['h', 'e', 'l', 'p'],), (['s', 't', 'a', 'k'],)]

This doesn't help at all.

Is there any way to "break up" a list int it's individual pieces for the zip function?

I need the list k, this is a simplified example, k would have an unknown amount of lists for where i'm using it.

Upvotes: 0

Views: 433

Answers (1)

Vivek Rai
Vivek Rai

Reputation: 910

Try the following code:

zip(*[lists...])

You can put in any number of lists in there (can easily be generated using list comprehension)

Upvotes: 3

Related Questions