A bug?

Denis McMahon denismfmcmahon at gmail.com
Tue Oct 28 22:17:27 EDT 2014


On Tue, 28 Oct 2014 01:29:28 +0000, Joshua Landau wrote:

> On 28 October 2014 00:36, Denis McMahon <denismfmcmahon at gmail.com>
> wrote:
>>
>> d = [[list(range(1,13))[i*3+j] for j in range(3)] for i in range(4)]
> 
> A quick note. Ranges (even 2.7's xrange) are all indexable. The cast to
> a list isn't needed.

Until you apply Chris' slicing trick, and then:

>>> [list(range(1,13))[i*3:i*3+3] for i in range(4)]
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
>>> [range(1,13)[i*3:i*3+3] for i in range(4)]
[range(1, 4), range(4, 7), range(7, 10), range(10, 13)]
>>> 

Depends how important it is that you get a list and not a range object ;)

(Thinking back to a recent discussion where someone was taking str(range
(x)) and not getting the expected results)

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list