On Aug 24, 12:44 am, beginner <[EMAIL PROTECTED]> wrote: > On Aug 24, 12:41 am, Davo <[EMAIL PROTECTED]> wrote: > > > > > > > On Aug 23, 9:24 pm, beginner <[EMAIL PROTECTED]> wrote: > > > > Hi All, > > > > How do I map a list to two lists with list comprehension? > > > > For example, if I have x=[ [1,2], [3,4] ] > > > > What I want is a new list of list that has four sub-lists: > > > > [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] > > > > [1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to > > > [3,4], [f(3), f(4)]. > > > > I just can't find any way to do that with list comprension. I ended up > > > using a loop (untested code based on real code): > > > > l=[] > > > for y in x: > > > l.append(y) > > > l.append([f(z) for z in y]) > > > > Thanks, > > > Geoffrey > > > This may be what you want: > > > l = [[y, [f(z) for z in y]] for y in x] > > > But It's a bit dense. How about: > > l=[] > > for y in x: > > Fy = [f(z) for z in y] > > l.extend([y, Fy]) > > > -- David- Hide quoted text - > > > - Show quoted text - > > This is exactly what I was looking for. Thanks.- Hide quoted text - > > - Show quoted text -
On second thought, that will generate one additional level. So it is not what I am looking for. -- http://mail.python.org/mailman/listinfo/python-list