Zip together each item from one list

Start with a list of lines. Each line will have the same number of characters, but this number is not specified, as well as the unrelated total number of lines.

Here is an example:

data = ['ABC', 'EFG', 'IJK', 'MNO']

My desired end result looks something like this:

[('A', 'E', 'I', 'M'), ('B', 'F', 'J', 'N'), ('C', 'G', 'K', 'O')]

My desired result is what I expect to see after being used zip()in some way. My problem is that I cannot find the right way to exchange each element of the same array together.

Essentially, I'm trying to get columns from a group of rows.

What I have tried so far:

  • Dividing data into a two-dimensional list:

    split_data = [list(row) for row in rows]

which gives me:

[['A', 'B', 'C'], ['E', 'F', 'G'], ['I', 'J', 'K'], ['M', 'N', 'O']]
  1. Trying to use zip()for this with something like:

    zip(split_data)

I continue as a result:

[(['A', 'B', 'C'],), (['E', 'F', 'G'],), (['I', 'J', 'K'],), (['M', 'N', 'O'],)]

, , , . zip() data split_data , ?

zip(data[0], data[1], data[2], ...)

+4
1

data:

>>> data = ['ABC', 'EFG', 'IJK', 'MNO']
>>> zip(*data)
[('A', 'E', 'I', 'M'), ('B', 'F', 'J', 'N'), ('C', 'G', 'K', 'O')]

, , @Martijn:

+6

All Articles