471,317 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

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 1051
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
is asking for.
--
Steven
Sep 17 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Chris Ritchey | last post: by
7 posts views Thread by Chris Ritchey | last post: by
2 posts views Thread by Girish Sahani | last post: by
1 post views Thread by Girish Sahani | last post: by
18 posts views Thread by Grant Edwards | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.