That's not the same, in the OP example you can assert d[0] is not d[1],
while in your code that assertion fails (the list comprehension evaluates
the expression each time creating a new list, your code makes 10 references
to a single 5 element list)


On 30 March 2017 at 14:51, Mark E. Haase <meha...@gmail.com> wrote:

> Your example is really repeating two things:
>
> d = [ [0 for _ in range(5)] for _ in range(10) ]
>
> But since list() uses * for repetition, you could write it more concisely
> as:
>
> d = [[0] * 5] * 10]
>
> I'm not picking on your specific example. I am only pointing out that
> Python gives you the tools you need to build nice APIs. If repetition is an
> important part of something you're working on, then consider using
> itertools.repeat, writing your own domain-specific repeat() method, or even
> override * like list() does. One of the coolest aspects of Python is how a
> relatively small set of abstractions can be combined to create lots of
> useful behaviors.
>
> For students, the lack of a "repeat" block might be confusing at first,
> but once the student understands for loops in general, it's an easy mental
> jump from "using the loop variable in the body" to "not using the loop
> variable in the body" to "underscore is the convention for an unused loop
> variable". In the long run, having one syntax that does many things is
> simpler than having many syntaxes that each do one little thing.
>
> On Thu, Mar 30, 2017 at 5:18 AM, Markus Meskanen <markusmeska...@gmail.com
> > wrote:
>
>> Hi Pythonistas,
>>
>> yet again today I ended up writing:
>>
>> d = [[0] * 5 for _ in range(10)]
>>
>> And wondered, why don't we have a way to repeat other than looping over
>> range() and using a dummy variable? This seems like a rather common thing
>> to do, and while the benefit doesn't seem much, something like this would
>> be much prettier and more pythonic than using underscore variable:
>>
>> d = [[0] * 5 repeat_for 10]
>>
>> And could obviously be used outside of comprehensions too:
>>
>> repeat_for 3:
>>     print('Attempting to reconnect...')
>>     if reconnect():
>>         break
>> else:
>>     print('Unable to reconnect :(')
>>     sys.exit(0)
>>
>> I chose to use repeat_for instead of repeat because it's way less likely
>> to be used as a variable name, but obviously other names could be used like
>> loop_for or repeat_times etc.
>>
>> Thoughts?
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas@python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>


-- 
Daniel F. Moisset - UK Country Manager
www.machinalis.com
Skype: @dmoisset
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to