[Tutor] Question on List Comprehensions

Charles Becker charleshbecker at gmail.com
Tue Nov 22 08:49:25 CET 2011

Alan, Steve, future readers,

After some re-reading and hacking I was able to discover the solution.  Since I raised the question here it is :

[['{0}'.format(x+1), x+1] for x in range(size)]

This will create the list with nested lists for whatever number 'size' is set to.  This should be good enough to get anyone started on future problems in this area, and yes the redundancy present does make sense in the scope of what I'm working on. :)

Thanks!  I'll leave future questions on the backburner a little longer.

Sent from my iPhone

On Nov 21, 2011, at 7:04 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:

> On 22/11/11 00:10, Steven D'Aprano wrote:
>> Because you don't have a list comprehension. You can't put add arbitrary
>> code inside a square brackets [ ]. You have to follow the syntax for a
>> list comprehension:
>> listcomp = [expression for name in sequence]
>> not
>> listcomp = [expression for name in sequence another_command]
> And being picky you can add a conditional after the loop:
> > listcomp = [expression for name in sequence if some_condition]
> But it must be an if test, nothing else will do.
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list