How do I make independent lists within in a list?

Sigurður Óskar Halldórsson :
seats = 4  # user can choose an even input, I put 4 for this example
rows = 4  # user can choose an even or odd input, I put 4 for this example
seats_in_row_list = [i for i in string.ascii_uppercase[:seats]]
main_seat_list = [seats_in_row_list for i in range(rows)]

The output is:

[['A', 'B', 'C', 'D'], ['A', 'B', 'C', 'D'], ['A', 'B', 'C', 'D'], ['A', 'B', 'C', 'D']]

But when I try to change 'A' to 'X' in the first list all of the lists change:

[['X', 'B', 'C', 'D'], ['X', 'B', 'C', 'D'], ['X', 'B', 'C', 'D'], ['X', 'B', 'C', 'D']] 

What I'm looking for is this output:

[['X', 'B', 'C', 'D'], ['A', 'B', 'C', 'D'], ['A', 'B', 'C', 'D'], ['A', 'B', 'C', 'D']]
omuthu :

Use copy method to have a copy of the individual list before assigning

main_seat_list = [seats_in_row_list.copy() for i in range(rows)]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=197993&siteId=1