#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Create a sublist of a list of list


    Hello everyone and happy Christmas !
    Here is my problem (I am a Python newbie):
    I have a list of list which consists of the following
    L1=[[label1,value1],[label2,value2], ...,[label15,value15]]
    I have another list which only contains a subset of labels e.g.
    L2=[label3,label9,label13]
    I want to create a subset of L1 only for the labels contained in L2.i
    L3=[[label3,value3],[label9,value9],[label13,value13]]
    Any idea on how to proceed ?
    Thanks
    Jacques
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481

    Merry Christmas


    Code:
    L3 = [A for A in L1 if A[0] in L2]
    
    # if the lists are long be sure to use sets.
    KEYS = set(L2)
    L3 = [A for A in L1 if A[0] in KEYS]
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0
    Thanks. Works great. I still have to understand the subtilities of this great language

IMN logo majestic logo threadwatch logo seochat tools logo