Iterating over a 2 dimensional python list

PythonLoopsMultidimensional Array

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

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

Solution 4 - Python

>>> 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')

Solution 5 - Python

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

Solution 6 - Python

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

Solution 7 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbhaskarcView Question on Stackoverflow
Solution 1 - PythonIliyan BobevView Answer on Stackoverflow
Solution 2 - PythonmrKelleyView Answer on Stackoverflow
Solution 3 - PythonAlexey KachayevView Answer on Stackoverflow
Solution 4 - PythonJoran BeasleyView Answer on Stackoverflow
Solution 5 - PythonAdrian RatnapalaView Answer on Stackoverflow
Solution 6 - PythonTanky WooView Answer on Stackoverflow
Solution 7 - PythondansalmoView Answer on Stackoverflow