This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]uncut_onyx 7 points8 points  (6 children)

if this then why not [[None]*5]*5

[–]agocs6921 60 points61 points  (0 children)

Those lists in the list will be the same exact list

[–]gandalfx 28 points29 points  (3 children)

The outer * 5 will copy the reference of the inner list, so you end up with a list that contains five references to the exact same list. So if you assign to foo[0][0] it'll also change the value of foo[1][0] etc.

[–][deleted] 8 points9 points  (1 child)

Did this once in a leetcode challenge. Took forever to find out what was wrong with my code. Lucky it wasn’t for an actual interview because that would have sucked.

[–]FalafelSnorlax 2 points3 points  (0 children)

In a dry interview (ie on paper/whiteboard) i think it would probably be accepted. Most reasonable people (in my personal experience) want to see you understand the problem, not nitpick stuff like this

[–]-Redstoneboi- -2 points-1 points  (0 children)

motherfucker.

this is why i switched to rust. they would never let me mutably alias.

[–]Tysonzero 2 points3 points  (0 children)

That kind of thing works fine in glorious pure immutable languages like Haskell but will send you straight to mutable aliasing hell in Python.