Search code examples
pythonlist-comprehension

remove lists containing less than 3 character strings


I have a list of lists

list_of_lists = [['stringxyz', ','], ['WER', 'stringz'], ['F.7.T.', 'UZ'], ['WWW', 'stringy'], [None, ''], [None, 'Frahm']]

that contains lists, that contain strings of character length <3 (see first and third lists above)

I want to remove those lists from the lists of lists

desired ouput:

list_of_lists = [['WER', 'stringz'], ['WWW', 'stringy']]


Solution

  • This works

    # use any to check if any sub-list has None in it or has an element shorter than 3 characters 
    [s_l for s_l in list_of_lists if not (None in s_l or any(len(x)<3 for x in s_l))]
    [['WER', 'stringz'], ['WWW', 'stringy']]