Python Forum

Full Version: List with equal repeatable data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi pythonists! This is my first post there... I faced with a question when I needed to create a list with a number of empty lists. I found two approaches:
1. ls = [[] for _ in range(9)]
2. ls = [[]] * 9

And after I printed them and their types I got the same results:

a = [[]] * 9
b = [[] for _ in range(9)]
print(a)
print(b)
print(type(a))
print(type(b))
results:
[[], [], [], [], [], [], [], [], []]
[[], [], [], [], [], [], [], [], []]
<class 'list'>
<class 'list'>

However when I was solving this task and checked the solution (available by the link too) from leetcode these lists worked in different ways - only with the second I got right result. What is the difference between them?
you should post your code (not original) using bbcode tags.
You may have introduced a typo.
(Jun-12-2024, 11:16 AM)Larz60+ Wrote: [ -> ]you should post your code (not original) using bbcode tags.
You may have introduced a typo.

This below is the solution which is not working. If uncomment 3 commented lines and replace three lines above with them it works.

class Solution(object):
    def isValidSudoku(self, board):
        rows = [[]] * 9
        columns = [[]] * 9
        box = [[]] * 9
        # rows = [[] for _ in range(9)]
        # columns = [[] for _ in range(9)]
        # box = [[] for _ in range(9)]

        for i in range(9):
            for j in range(9):
                num = board[i][j]
                if num != ".":
                    box_index = (i // 3) * 3 + (j // 3)
                    if num in rows[i] or num in columns[j] or num in box[box_index]:
                        return False
                    rows[i].append(num)
                    columns[j].append(num)
                    box[box_index].append(num)

        return True
It looks like a problem similar to this one
a = [[]] * 9
b = [[] for _ in range(9)]
a == b
True
Pedroski55 this do not show the problem here.
a is the wrong solution,even if is it equals to b when test.
Test with is.
 >>> a is b
False 
So a and b will behave different,as a every sublist point to same memory object.
>>> a[0].append(9)
>>> a
[[9], [9], [9], [9], [9], [9], [9], [9], [9]]
>>> 
>>> b[0].append(9)
>>> b
[[9], [], [], [], [], [], [], [], []]
Ataman for the sudok solution use this way to create the lists [[] for _ in range(9)]
Now each sublist will be a separate object in memory.
class Solution(object):
    def valid_sudoku(self, board):
        rows = [[] for _ in range(9)]
        columns = [[] for _ in range(9)]
        box = [[] for _ in range(9)] 
        for i in range(9):
            for j in range(9):
                num = board[i][j]
                if num != ".":
                    box_index = (i // 3) * 3 + (j // 3)
                    if num in rows[i] or num in columns[j] or num in box[box_index]:
                        return False
                    rows[i].append(num)
                    columns[j].append(num)
                    box[box_index].append(num) 
        return True 
snippsat, Gribouillis, thank you guys for the detailed explanation!