Thursday, May 31, 2018

Classic Python Gotch - * on List of Lists - No No!!

[0] * 10 

is okay.

But,

initializing using

[ [0] ] * 10
So you get [ [0] , [0], ... [0] ] 
is NOT okay - you've built a list of copies of the SAME list!!!

You wouldn't guess would you? This is where Larry Wall would have dumped out a nice warning. Why can't python marry with Perl?

No comments: