Home > Software engineering >  python filter out NoneType in mixed list
python filter out NoneType in mixed list

Time:02-18

Have this list from ldap search result, trying to parse to csv. But have to filter out None's first. But getting just the same content as search_res. Something I'm doing wrong?

search_res:

[('CN=GON,OU=App,OU=Groups,DC=com', {'member': [b'CN=user1,OU=Users,DC=com', b'CN=user2,OU=Users,DC=com',]}), (None, ['ldap://skogen.com/CN=Sche,CN=Conf,DC=com']), (None, ['ldap://skogen.com/CN=Sche,CN=Conf,DC=com'])]

lambda:

lame = lambda x: (x is not None)
list_out = list(filter(lame, search_res))
print(list_out)

What I want to my csv is just:

user1
user2

CodePudding user response:

The entries of your list are tuples, and your are checking if the tuples are None. However, it looks like you want to check that the first element of your tuples are not None. So you have change your filter function as

lame = lambda x: (x and x[0] is not None)

This now checks both, for entire None elements and for the first element not being None.

  • Related