1

I have this list with sublists and I wanted do create two list beacuse the length of each sublist is 2. The first sublist contains every first element of each sublist, and the second one contains the second elements of each sublist.

For exemple, assuming this is the input:

col=[[' ', 2], [1, 2], [' ', 2], [' ', 3], [' ', 3]]

I want a list like this:

col= [[' ', 1, ' ', ' ', ' '],[2 ,1, 2, 3, 3]]

I tried:

for i in range(len(col)-1):
      col[i][0]= col[i][0] + col[i+1][0]

The problem is that even if this worked it would be only for a list conatining sublists with len 2, but I needed something that would work for any len of the sublists. For exemple if I had:

ss= [[2,4,5,2],[5,6,7,6],[4,8,9,4]]

I would obtain:

ss=[[2,5,4],[4,6,8],[5,7,9],[2,6,4]]
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
marga
  • 11
  • 2

1 Answers1

3

Try using zip

>>> print zip(*ss)
[(2, 5, 4), (4, 6, 8), (5, 7, 9), (2, 6, 4)]
Colin
  • 2,087
  • 14
  • 16