{"type":"rich","version":"1.0","provider_name":"Transistor","provider_url":"https://transistor.fm","author_name":"Programming Tech Brief By HackerNoon","title":"Why * Doesn't Create Independent Nested Lists in Python (The Hidden List Multiplication Trap)","html":"<iframe width=\"100%\" height=\"180\" frameborder=\"no\" scrolling=\"no\" seamless src=\"https://share.transistor.fm/e/87e96298\"></iframe>","width":"100%","height":180,"duration":1048,"description":"\n        This story was originally published on HackerNoon at: https://hackernoon.com/why-doesnt-create-independent-nested-lists-in-python-the-hidden-list-multiplication-trap.\nLearn why [[0,0,0]] * 3 in Python secretly shares one list across all rows, and how to build truly independent nested lists the right way.\nCheck more stories related to programming at: https://hackernoon.com/c/programming.\n            You can also check exclusive content about #python, #python-programming, #python-lists, #python-shared-references, #python-mutable-objects, #python-list-comprehension, #mutable-list-bug-in-python, #hackernoon-top-story,  and more.\nThis story was written by: @sohelalam79. Learn more about this writer by checking @sohelalam79's about page,\n            and for more stories, please visit hackernoon.com.\n[x] * n does not create n independent copies of x. It creates one object and repeats a reference to that same object n times. For immutable elements like numbers or strings, this is harmless because any \"change\" actually rebinds the slot to a new object. For mutable elements like lists or dicts, every slot points to the exact same object, so mutating one slot mutates all of them. The fix is to use a list comprehension — [x for _ in range(n)] — which creates a genuinely new object on each iteration. Key takeaway: * repeats references, not values.","thumbnail_url":"https://img.transistorcdn.com/KhCapPSRkLGL2Xw8888yuChkNRWthaKapLYTvNdu4W4/rs:fill:0:0:1/w:400/h:400/q:60/mb:500000/aHR0cHM6Ly9pbWct/dXBsb2FkLXByb2R1/Y3Rpb24udHJhbnNp/c3Rvci5mbS9zaG93/LzQxMTY2LzE2ODM1/ODIzMzAtYXJ0d29y/ay5qcGc.webp","thumbnail_width":300,"thumbnail_height":300}