Python Forum
List with equal repeatable data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List with equal repeatable data
#1
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?
Reply
#2
you should post your code (not original) using bbcode tags.
You may have introduced a typo.
Reply
#3
(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
Reply
#4
It looks like a problem similar to this one
« We can solve any problem by introducing an extra level of indirection »
Reply
#5
a = [[]] * 9
b = [[] for _ in range(9)]
a == b
True
Reply
#6
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 
Pedroski55 likes this post
Reply
#7
snippsat, Gribouillis, thank you guys for the detailed explanation!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 655 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Check if two matrix are equal and of not add the matrix to the list quest 3 984 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  Regarding how to randomizing list with having equal probability radraw 14 2,528 Nov-06-2022, 11:09 PM
Last Post: Pedroski55
  detect equal sequences in list flash77 17 3,301 Oct-28-2022, 06:38 AM
Last Post: flash77
  is there equal syntax to "dir /s /b" kucingkembar 2 1,113 Aug-16-2022, 08:26 AM
Last Post: kucingkembar
  Can a variable equal 2 things? Extra 4 1,645 Jan-18-2022, 09:21 PM
Last Post: Extra
  Why changing data in a copied list changes the original list? plumberpy 3 2,393 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  Not equal a dictionary key value bazcurtis 2 2,039 Dec-11-2019, 11:15 PM
Last Post: bazcurtis
  Misunderstanding with the “if” statement and “not equal” scriptoghost 6 4,601 Jun-23-2017, 09:43 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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