Python Forum

Full Version: while iterating a list, it changes all the elements at once
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a 3x3 square that is filled with None values [[None, None, None], [None, None, None], [None, None, None]]
I want to fill the first row and column with 1s, how do I go about and do this?

I've been trying some stuff but for some reason when I iterate one row at a time, the operation goes on all rows at once
Please, post your full code in code tags. It sounds like something related to the way you crate the list.
I did some tinkering around trying to fix it so it's not complete but the problem is still here in this version

the first blocks are for making a square with mXm dimension

m = 3
n = 1


square = []
row = []
for i in range(m):             #maakt row en kolom van dimensie m
    row.append(None)
    square.append(None)

for i in square:
    noneindex = square.index(None)  #pakt eerste None
    square.insert(noneindex,row)   #voegt row toe op plek na None
    square.pop(noneindex + 1)           #verwijdert None

print(square)

for list in square:
        for element in  list:
            if list.index(element) == 0:
                list.insert(lijst.index(None),n)
                list.pop(1)





print(square)
the way im making a mXm square is probably VERY non-pythonic, i'm sorry in advance ^_^
How do you point the first element in a list... my_list[0]. So iterate over it and change the values.
in this code, everything works as intended, row and column is replaced with 1s
vierkant = [[None, None, None], [None, None, None], [None, None, None]]

for lijst in vierkant:
    if vierkant.index(lijst) == 0:
        for index, element in enumerate(lijst):
           lijst[index] = 1

    else:
        for index, element in enumerate(lijst):
            if index == 0:
                lijst[index] = 1
print(vierkant)
but in this code, which is exactly the same but with a algorithm for generating mXm squares, it all changes at once
m = 3
n = 1


vierkant = []
rij = []
for i in range(m):             #maakt rij en kolom van dimensie m
    rij.append(None)
    vierkant.append(None)

for i in vierkant:
    noneindex = vierkant.index(None)  #pakt eerste None
    vierkant.insert(noneindex,rij)   #voegt rij toe op plek na None
    vierkant.pop(noneindex + 1)           #verwijdert None

print(vierkant)

for lijst in vierkant:
    if vierkant.index(lijst) == 0:
        for index, element in enumerate(lijst):
           lijst[index] = 1

    else:
        for index, element in enumerate(lijst):
            if index == 0:
                lijst[index] = 1

print(vierkant)
vierkant = square
rij = row
lijst = list
if you're wondering

I hope people are still lurking this is driving me insane
this is common gotcha for newbies in python
 
>>> l1 = [1,2,3]
>>> l2 = l1
>>> l1
[1, 2, 3]
>>> l2
[1, 2, 3]
>>> l1[0]=4
>>> l1
[4, 2, 3]
>>> l2
[4, 2, 3]
>>> l2 = l1[::]
>>> l1
[4, 2, 3]
>>> l2
[4, 2, 3]
>>> l1[1]=5
>>> l1
[4, 5, 3]
>>> l2
[4, 2, 3]
the correct way to create list of lists
>>> row = [None]*3
>>> row
[None, None, None]
>>> square = [row[::] for _ in range(3)]
>>> square
[[None, None, None], [None, None, None], [None, None, None]]
>>> square[0][0]=1
>>> square
[[1, None, None], [None, None, None], [None, None, None]]
(Nov-26-2017, 09:06 PM)buran Wrote: [ -> ]this is common gotcha for newbies in python
 
>>> l1 = [1,2,3]
>>> l2 = l1
>>> l1
[1, 2, 3]
>>> l2
[1, 2, 3]
>>> l1[0]=4
>>> l1
[4, 2, 3]
>>> l2
[4, 2, 3]
>>> l2 = l1[::]
>>> l1
[4, 2, 3]
>>> l2
[4, 2, 3]
>>> l1[1]=5
>>> l1
[4, 5, 3]
>>> l2
[4, 2, 3]
the correct way to create list of lists
>>> row = [None]*3
>>> row
[None, None, None]
>>> square = [row[::] for _ in range(3)]
>>> square
[[None, None, None], [None, None, None], [None, None, None]]
>>> square[0][0]=1
>>> square
[[1, None, None], [None, None, None], [None, None, None]]

thanks dude, you guys make the world a better place
you can address elements in a 2D list by just selecting the element you want to change.
example:
list = [[None, None, None], [None, None, None], [None, None, None]]
for i in range(0,3):
    list[0][i] = 1
## list == [[1, 1, 1], [None, None, None], [None, None, None]]

#to change every element with scaling lists
for i in range(0, len(list)):
    for j in range(0, len(list[i]):
        list[i][j] = 1
# list == [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
(made code off top of my head, it may not work correctly but its how i did it last time i used it. Also there is an issue with how you create the list using this method see https://python-forum.io/Thread-Change-si...5#pid30355 for an explanation on this)