70

I have created a 2 dimension array like:

rows =3
columns= 2
mylist = [[0 for x in range(columns)] for x in range(rows)]
for i in range(rows):
    for j in range(columns):
        mylist[i][j] = '%s,%s'%(i,j)
print mylist

Printing this list gives an output:

[  ['0,0', '0,1'], ['1,0', '1,1'], ['2,0', '2,1']   ]

where each list item is a string of the format 'row,column'

Now given this list, i want to iterate through it in the order:

'0,0'
'1,0'
'2,0'
'0,1'
'1,1'
'2,1'

that is iterate through 1st column then 2nd column and so on. How do i do it with a loop ?

This Question pertains to pure python list while the question which is marked as same pertains to numpy arrays. They are clearly different

Jim G.
  • 15,141
  • 22
  • 103
  • 166
bhaskarc
  • 9,269
  • 10
  • 65
  • 86

7 Answers7

137

same way you did the fill in, but reverse the indexes:

>>> for j in range(columns):
...     for i in range(rows):
...        print mylist[i][j],
... 
0,0 1,0 2,0 0,1 1,1 2,1
>>> 
Iliyan Bobev
  • 3,070
  • 2
  • 20
  • 24
  • 24
    This should be the accepted answer, because it is simple and easy to understand. One may not be familiar with the clever functions used in the other answers, but with only a quick look at the code, it's obvious how this solution works. Because of that, this answer is the most Pythonic. – Mr. Lance E Sloan Nov 05 '13 at 12:28
  • 2
    Hmm.. What if each of the children lists doesn't have the save length (rows here) ? – sonlexqt Dec 13 '16 at 17:45
  • @sonlexqt iterate indexes of the longest row and use `try ... except IndexError` around the print operand. (On my cell so can't provide exact example) – Iliyan Bobev Dec 14 '16 at 03:44
  • But this doesn't work if the number of rows is more than the number of columns. – TamingofTheShru Dec 19 '20 at 19:11
102

This is the correct way.

>>> x = [ ['0,0', '0,1'], ['1,0', '1,1'], ['2,0', '2,1'] ]
>>> for i in range(len(x)):
        for j in range(len(x[i])):
                print(x[i][j])


0,0
0,1
1,0
1,1
2,0
2,1
>>> 
Kenneth Reitz
  • 8,559
  • 4
  • 31
  • 34
mrKelley
  • 3,365
  • 2
  • 21
  • 28
38

Use zip and itertools.chain. Something like:

>>> from itertools import chain
>>> l = chain.from_iterable(zip(*l))
<itertools.chain object at 0x104612610>
>>> list(l)
['0,0', '1,0', '2,0', '0,1', '1,1', '2,1']
Alexey Kachayev
  • 6,106
  • 27
  • 24
7
>>> mylist = [["%s,%s"%(i,j) for j in range(columns)] for i in range(rows)]
>>> mylist
[['0,0', '0,1', '0,2'], ['1,0', '1,1', '1,2'], ['2,0', '2,1', '2,2']]
>>> zip(*mylist)
[('0,0', '1,0', '2,0'), ('0,1', '1,1', '2,1'), ('0,2', '1,2', '2,2')]
>>> sum(zip(*mylist),())
('0,0', '1,0', '2,0', '0,1', '1,1', '2,1', '0,2', '1,2', '2,2')
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
4

zip will transpose the list, after that you can concatenate the outputs.

In [3]: zip(*[ ['0,0', '0,1'], ['1,0', '1,1'], ['2,0', '2,1'] ])
Out[3]: [('0,0', '1,0', '2,0'), ('0,1', '1,1', '2,1')]
Adrian Ratnapala
  • 5,485
  • 2
  • 29
  • 39
3

Ref: zip built-in function

zip() in conjunction with the * operator can be used to unzip a list

unzip_lst = zip(*mylist)
for i in unzip_lst:
    for j in i:
        print j
Tanky Woo
  • 4,906
  • 9
  • 44
  • 75
1
>>> [el[0] if i < len(mylist) else el[1] for i,el in enumerate(mylist + mylist)]
['0,0', '1,0', '2,0', '0,1', '1,1', '2,1']
dansalmo
  • 11,506
  • 5
  • 58
  • 53