[Tutor] Multiple lists from single list with nested lists

Sander Sweers sander.sweers at gmail.com
Tue Oct 21 23:09:35 CEST 2008


On Tue, Oct 21, 2008 at 01:36, Kent Johnson <kent37 at tds.net> wrote:
>> somelist = ['Test1', 'Text2', ['1', '2'], 'Text3']
>> templist = []
>> for x in range(len(somelist[2])):
>>    templist.append([somelist[0], somelist[1], somelist[2][x], somelist[3]])
>
> Is it always just the third item that is a list? If so I think your
> solution is not too bad.

The actual list is 12 items long (python lenght) and the 6, 7 and 8th
item should be lists after I split them. A sanitized example:

['SOMENAME', '1234567890', '"TEXT1"', 'TEXT2', '59', '',
'10/03;10/15', '10/09;10/20', '1;70', '', '', '']

> or use a list comprehension:
> templist = [ somelist[0:2] + [x] + somelist[3:] for x in somelist ]

This did not work but the below did ;-)
templist = [ somelist[:1] + [x] + somelist[3:] for x in somelist[2] ]

It gave me some great pointers on how to handle this.

Thanks
Sander


More information about the Tutor mailing list