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
>>> 
Answer from Iliyan Bobev on Stack Overflow
๐ŸŒ
Snakify
snakify.org โ€บ two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
To process 2-dimensional array, you typically use nested loops. The first loop iterates through the row number, the second loop runs through the elements inside of a row. For example, that's how you display two-dimensional numerical list on ...
Discussions

Iterating through a two dimensional array in Python? - Stack Overflow
I'm trying to iterate through a two dimensional array in Python and compare items in the array to ints, however I am faced with a ton of various errors whenever I attempt to do such. I'm using nump... More on stackoverflow.com
๐ŸŒ stackoverflow.com
create a dynamic two dimensional array in python (loop) - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
June 12, 2017
Nested for Loops in Python- creating a two dimensional array/list of lists - Stack Overflow
New to Python here. I just need a little bit of clarification on how the first "list of lists" is all 0's in the output below...is the first iteration of a loop the zeroth iteration? #co... More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 9, 2020
python 3.x - Two-dimensional arrays and for-loops - Stack Overflow
The following code is part of a larger programme. It has caused a problem, whilst trying to debug it I decided to print the two-dimensional array 'posB' and it keeps changing with every iteration o... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Medium
medium.com โ€บ an-amygdala โ€บ how-to-iterate-through-a-2d-list-in-python-5a90693f3a15
How to Iterate Through a 2D List in Python | by an amygdala | An Amygdala | Medium
August 10, 2020 - First, the list is assigned to a variable called data. Then we use a for loop to iterate through each element in the range of the list. Unless we specify a starting index for the range, it defaults to the first element of the list.
Top answer
1 of 2
2

You need to tell us something about this: dataset = datas.values

It's probably a 2d array, since it derives from a load of a csv. But what shape and dtype? Maybe even a sample of the array.

Is that the data argument in the function?

What are blackKings and values? You treat them like lists (with append).

for i in data:
    if data[i][39] == 1:

This doesn't make sense. for i in data, if data is 2d, i is the the first row, then the second row, etc. If you want i to in an index, you use something like

for i in range(data.shape[0]):

2d array indexing is normally done with data[i,39].

But in your case data[i][39] is probably an array.

Anytime you use an array in a if statement, you'll get this ValueError, because there are multiple values.

If i were proper indexes, then data[i,39] would be a single value.

To illustrate:

In [41]: data=np.random.randint(0,4,(4,4))
In [42]: data
Out[42]: 
array([[0, 3, 3, 2],
       [2, 1, 0, 2],
       [3, 2, 3, 1],
       [1, 3, 3, 3]])
In [43]: for i in data:
    ...:     print('i',i)
    ...:     print('data[i]',data[i].shape)
    ...:     
i [0 3 3 2]            # 1st row
data[i] (4, 4)
i [2 1 0 2]            # a 4d array
data[i] (4, 4)
...

Here i is a 4 element array; using that to index data[i] actually produces a 4 dimensional array; it isn't selecting one value, but rather many values.

Instead you need to iterate in one of these ways:

In [46]: for row in data:
    ...:     if row[3]==1:
    ...:         print(row)
[3 2 3 1]
In [47]: for i in range(data.shape[0]):
    ...:     if data[i,3]==1:
    ...:         print(data[i])
[3 2 3 1]

To debug a problem like this you need to look at intermediate values, and especially their shapes. Don't just assume. Check!

2 of 2
0

I'm going to attempt to rewrite your function

def model_building(data):
    global blackKings
    blackKings.append(data[0, 1])

    # Your nested if statements were performing an xor
    # This is vectorized version of the same thing
    values = np.logical_xor(*(data.T[[39, 40]] == 1)) * -2 + 1

    # not sure where `values` is defined.  If you really wanted to
    # append to it, you can do
    # values = np.append(values, np.logical_xor(*(data.T[[39, 40]] == 1)) * -2 + 1)

    # Your blackKings / flag logic can be reduced
    mask = (blackKings[:, None] != data[:, 1]).all(1)
    blackKings = np.append(blackKings, data[:, 1][mask])

This may not be perfect because it is difficult to parse your logic considering you are missing some pieces. But hopefully you can adopt some of what I've included here and improve your code.

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python_data_structure โ€บ python_2darray.htm
Python - 2-D Array
To print out the entire two dimensional array we can use python for loop as shown below.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ 2d array in python
2D Array in Python | Python Two-Dimensional Array - Scaler Topics
May 25, 2026 - So for each inner array, we run a loop to traverse its elements. As you can see in the above image, the arrows are marked in sequence. The first row is traversed horizontally, then we come down to the second row, traverse it, and finally come down to the last row, traversed.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-using-2d-arrays-lists-the-right-way
Using 2D arrays/lists in Python - GeeksforGeeks
Python creates only one inner list and one 0 object, not separate copies. This shared reference behavior is known as shallow copying (aliasing). If we assign the 0th index to another integer say 1, then a new integer object is created with the value of 1 and then the 0th index now points to this new int object as shown below ยท Similarly, when we create a 2d array as "arr = [[0]*cols]*rows" we are essentially extending the above analogy.
Published ย  December 20, 2025
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 39487235 โ€บ create-a-dynamic-two-dimensional-array-in-python-loop
create a dynamic two dimensional array in python (loop) - Stack Overflow
June 12, 2017 - i am trying to create a two-dimensional array in python..In my php i have this code: $i = 1; $arr= array(); foreach($mes as $res){ $arr[$i]->type = $res->item; ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 64287450 โ€บ nested-for-loops-in-python-creating-a-two-dimensional-array-list-of-lists
Nested for Loops in Python- creating a two dimensional array/list of lists - Stack Overflow
October 9, 2020 - # collect input from the user as integers X = int(input("Enter a Value for 'X': ")) Y = int(input("Enter a Value for 'Y': ")) print("") # define the outermost list as an empty list outerlist = [] # outermost loop should control the outermost list # create that one first...outerlist for i in range(1, X + 1): # now create the innerlist innerlist = [] # append the innerlist 'Y' number of times for j in range(1, Y + 1): innerlist.append(i * j) outerlist.append(innerlist) print(outerlist)
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 54485845 โ€บ two-dimensional-arrays-and-for-loops
python 3.x - Two-dimensional arrays and for-loops - Stack Overflow
The following code is part of a larger programme. It has caused a problem, whilst trying to debug it I decided to print the two-dimensional array 'posB' and it keeps changing with every iteration o...
๐ŸŒ
Programmingforlovers
programmingforlovers.com โ€บ home โ€บ chapter 3: discovering a self-replicating automaton with top-down programming โ€บ chapter 3 python code alongs โ€บ introduction to two-dimensional arrays in python
Introduction to Two-Dimensional Arrays in Python - Programming for Lovers
March 17, 2026 - Python provides a number of ways ... show one. After making a blank list a, we will use a for loop to range over the number of rows that we want to create, and in each one, we create the row, and then append it to a....
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ programming
Two dimensional array - Python
February 23, 2021 - Write a Python program which takes two digits as input and generates a two dimentional array. row = int(input('Enter the number of row :')) col = int(input('Enter the number of columns : ')) multi_list = [[0 for col in range(col)], [0 for row in range(row)]] for i in range(row): for j in range(col): multi_list[row][col] = row * col print(multi_list) error message : Syntax Error: invalid syntax (for i in range(row))
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 69962739 โ€บ plot-two-dimensional-array-with-for-loop
python - Plot two dimensional array with "for" loop - Stack Overflow
Essentially, what you need to do is go through your x list (the structures declared with square brackets in Python are lists, not arrays ๐Ÿ˜‰) and, for each element, calculate either of those sine formulae you presented.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_iterating.asp
NumPy Array Iterating
To return the actual values, the scalars, we have to iterate the arrays in each dimension. ... import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) for x in arr: for y in x: for z in y: print(z) Try it Yourself ยป ยท The function nditer() is a helping function that can be used from very basic to very advanced iterations. It solves some basic issues which we face in iteration, lets go through it with examples. In basic for loops, iterating through each scalar of an array we need to use n for loops which can be difficult to write for arrays with very high dimensionality.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how can i iterate through the outermost elements of a 2d array?
r/learnprogramming on Reddit: How can I iterate through the outermost elements of a 2D array?
April 2, 2021 -

How can iterate through only the outermost objects of a 2d array? If I had a 5x5 array of integers all set to 0, how can iterate through and change the outermost objects to a different value using for loops?

So if I had an array that looked like:

00000
00000
00000
00000
00000

How can I change it to look like:

11111
10001
10001
10001
11111

I'm looking for a more conceptual answer but an example in a language like java or python would also be helpful.

๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1810901 โ€บ two-dimensional-array-display-using-two-for-loops
Two dimensional array display using two for loops. | Sololearn: Learn to code for FREE!
The outer loop (0 - 1) goes through the arrays, the inner loop goes through the values of this array. i = 0 -> j = 0, j = 1, j = 2 i = 1 -> j = 0, j = 1, j = 2 print sample[i][j] -> sample[0][0], sample[0][1], sample[0][2] ... sample[1][2] ... ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ preserving 2d list in for loops
r/learnpython on Reddit: preserving 2d list in for loops
April 25, 2023 -

Very early into my newest hobby, learning to code so forgive my poor etiquette

Trying to figure out when iterating through a 2d list with a for loop

This structure doesn't preserve the 2d list

new_list = []
for temp_var1 in two_d_lst:
    for temp_var2 in temp_var1:
        new_list.append(temp_var2.method())

But this structure does

new_list = []
for temp_var1 in two_d_lst:
    unpacking_lst = []
    for temp_var2 in temp_var1:
        unpacking_lst.append(temp_var2.method())
    new_list.append(unpacking_lst)

I'm not sure I understand why...

Greatly appreciate sarcastic remarks, especially if it helps me understand this.