Python Forum
while iterating a list, it changes all the elements at once
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while iterating a list, it changes all the elements at once
#1
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
Reply
#2
Please, post your full code in code tags. It sounds like something related to the way you crate the list.
Reply
#3
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 ^_^
Reply
#4
How do you point the first element in a list... my_list[0]. So iterate over it and change the values.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
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
Reply
#6
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]]
Reply
#7
(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
Reply
#8
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Checking if a string contains all or any elements of a list k1llcod3 1 1,023 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 1,912 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,014 May-17-2022, 11:38 AM
Last Post: Larz60+
  Why am I getting list elements < 0 ? Mark17 8 3,034 Aug-26-2021, 09:31 AM
Last Post: naughtyCat
  Looping through nested elements and updating the original list Alex_James 3 2,070 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Extracting Elements From A Website List knight2000 2 2,182 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Make Groups with the List Elements quest 2 1,936 Jul-11-2021, 09:58 AM
Last Post: perfringo
  Delete list while iterating shantanu97 1 1,866 Jun-06-2021, 11:59 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020