1

Today I noticed, I couldn't copy a list. Nor using .copy() method, nor slicing. It always refered to both of lists.

I worked around by creating same list by list comprehension but for some reason common methods didn't work.

Could you explain why my code didn't work correctly with copy() / slicing?

Note: 2 lists differ with contents but it shouldn't matter because my plan was to change every item of them based on conditions written later in the program.

def find_next_gen(generation):
    generation2 = [['.' for x in range(n)] for x in range(m)]
    # generation2 = generation1[:] <- didn't work
    # generation2 = generation1.copy() <- didn't work

    for i, row in enumerate(generation1):
        for j, element in enumerate(row):

            # checking alive_neighbours:
            alive_neighbours = 0

            # same row:
            alive_neighbours += check(generation1[i][j - 1] if j != 0 else generation1[i][-1])
            alive_neighbours += check(generation1[i][j + 1] if j != n - 1 else generation1[i][0])

            # row above:
            if i == 0:
                row_num_above = -1
            else:
                row_num_above = i - 1
            alive_neighbours += check(generation1[row_num_above][j - 1] if j != 0 else generation1[row_num_above][-1])
            alive_neighbours += check(generation1[row_num_above][j])
            alive_neighbours += check(generation1[row_num_above][j + 1] if j != n - 1 else generation1[row_num_above][0])

            # row beneath:
            if i == m - 1:
                row_num_beneath = 0
            else:
                row_num_beneath = i + 1
            alive_neighbours += check(generation1[row_num_beneath][j - 1] if j != 0 else generation1[row_num_beneath][-1])
            alive_neighbours += check(generation1[row_num_beneath][j])
            alive_neighbours += check(generation1[row_num_beneath][j + 1] if j != n - 1 else generation1[row_num_beneath][0])

            # create new generation:
            if generation1[i][j] == 'X' and 3 >= alive_neighbours >= 2 or generation1[i][j] == '.' and alive_neighbours == 3:
                generation2[i][j] = 'X'
            else:
                generation2[i][j] = '.'
    return generation2
konradz
  • 23
  • 1
  • 8

1 Answers1

1

You need copy.deepcopy() to copy nested lists. Otherwise you'll successfully copy the top-level list, but the elements of the new copy will still refer to the same sub-lists.

Amber
  • 507,862
  • 82
  • 626
  • 550