473,473 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1100
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
2
by: Girish Sahani | last post by:
hello ppl, Consider a list like . Here 'a','b','c' are objects and 1,3,4,2 are their instance ids and they are unique e.g. a.1 and b.1 cannot exist together. From this list i want to generate...
1
by: Girish Sahani | last post by:
hello ppl, Consider a list like . Here 'a','b','c' are objects and 1,3,4,2 are their instance ids and they are unique e.g. a.1 and b.1 cannot exist together. From this list i want to generate...
18
by: Grant Edwards | last post by:
Could whoever is responsible for the gateway that is grabbing my postings off of Usenet and e-mailing them out please fix the headers in the mail messages so that I don't get the bounce messages?...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.