<div dir="auto"><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Mar 30, 2017 19:04, "Joao S. O. Bueno" <<a href="mailto:jsbueno@python.org.br">jsbueno@python.org.br</a>> wrote:<br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="quoted-text">On 30 March 2017 at 10:51, Mark E. Haase <<a href="mailto:mehaase@gmail.com">mehaase@gmail.com</a>> wrote:<br>
> Your example is really repeating two things:<br>
><br>
> d = [ [0 for _ in range(5)] for _ in range(10) ]<br>
><br>
> But since list() uses * for repetition, you could write it more concisely<br>
> as:<br>
><br>
> d = [[0] * 5] * 10]<br>
><br>
> I'm not picking on your specific example. I am only pointing out that Python<br>
> gives you the tools you need to build nice APIs. If repetition is an<br>
> important part of something you're working on, then consider using<br>
> itertools.repeat, writing your own domain-specific repeat() method, or even<br>
> override * like list() does. One of the coolest aspects of Python is how a<br>
> relatively small set of abstractions can be combined to create lots of<br>
> useful behaviors.<br>
<br>
</div>I find it weird that not the author, neither the previous repliers noticed that<br>
"a repetition other than a for with dummy variable" was already in plain sight,<br>
in the very example given.<br>
Of course one is also free to write [ [0 for _ in range(5)] for _ in<br>
range(10)] if he wishes so.<br>
</blockquote></div><br></div></div><div class="gmail_extra" dir="auto">Had you read all the replies, you'd see people (including me, OP) repeating this multiple times:</div><div class="gmail_extra" dir="auto"><br></div><div class="gmail_extra" dir="auto">d = [[0] * 5] * 10</div><div class="gmail_extra" dir="auto"><br></div><div class="gmail_extra" dir="auto">Creates a list of ten references *to the same list*. This means that if I mutate any of the sub lists in d, all of the sub lists get mutated. There would only be one sub list, just ten references to it.</div></div>