472,983 Members | 2,486 Online

# generating list of sub lists

Hi,

is there a one-liner to accomplish the following task?
>From the list
l = ['string1', 'string2', 'string3']
generate the list of lists
l = [['string1'], ['string1', 'string2'], ['string1', 'string2',
'string3']]

Any help would be appreciated.

Thanks
Francesco

Sep 16 '07 #1
5 1091
cesco wrote:
l = ['string1', 'string2', 'string3']
l2=[l[:i+1] for i in range(len(l))]

Sep 16 '07 #2
On 9/16/07, cesco <fd**********@gmail.comwrote:
Hi,

is there a one-liner to accomplish the following task?
From the list
l = ['string1', 'string2', 'string3']
generate the list of lists
l = [['string1'], ['string1', 'string2'], ['string1', 'string2',
'string3']]

Any help would be appreciated.

Thanks
Francesco
>>l = [1,2,3,4,5]
>>[l[:i] for i in range(len(l))]
[[], [1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]
>>>
well almost works except for the first empty list. [Are you sure you
dont want it?]

Corrected
>>[l[:i+1] for i in range(len(l)-1)]
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]

Though I wonder if there is as neat a way as the first?
Sep 16 '07 #3
Rustom Mody wrote:
On 9/16/07, cesco <fd**********@gmail.comwrote:
>Hi,

is there a one-liner to accomplish the following task?
>From the list
l = ['string1', 'string2', 'string3']
generate the list of lists
l = [['string1'], ['string1', 'string2'], ['string1', 'string2',
'string3']]

Any help would be appreciated.

Thanks
Francesco
>>>l = [1,2,3,4,5]
>>>[l[:i] for i in range(len(l))]
[[], [1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]
well almost works except for the first empty list. [Are you sure you
dont want it?]

Corrected
>>>[l[:i+1] for i in range(len(l)-1)]
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]

Though I wonder if there is as neat a way as the first?
>>[l[:i] for i in range(1, len(l))]
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]
>>>
seems a slightly neater way to meet the requirement.

regards
steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline

Sep 16 '07 #4
cesco wrote:
Hi,

is there a one-liner to accomplish the following task?
>>From the list
l = ['string1', 'string2', 'string3']
generate the list of lists
l = [['string1'], ['string1', 'string2'], ['string1', 'string2',
'string3']]

Any help would be appreciated.

Thanks
Francesco
l = [l, l]

Ross
Sep 17 '07 #5
On Mon, 17 Sep 2007 20:52:14 +1000, Ross Wilson wrote:
cesco wrote:
>Hi,

is there a one-liner to accomplish the following task?
>>>From the list
l = ['string1', 'string2', 'string3'] generate the list of lists
l = [['string1'], ['string1', 'string2'], ['string1', 'string2',
'string3']]

Any help would be appreciated.

Thanks
Francesco

l = [l, l]

Ross

Did you *try* your suggestion before posting?
>>l = ['string1', 'string2', 'string3']
assert [l, l] == [['string1'], ['string1', 'string2'],
.... ['string1', 'string2', 'string3']]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
What you've suggested doesn't even come close to what the Original Poster