generating list of sub lists

C

cesco

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
 
R

Rustom Mody

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?
 
S

Steve Holden

Rustom said:
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
 
R

Ross Wilson

cesco said:
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
 
S

Steven D'Aprano

cesco said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top