How to remove empty tuples from a list of tuples in Python

2 Answers

0 votes
def remove_empty_tuples(lst_tpl): 
    new_lst_tpl = [e for e in lst_tpl if e] 
    return new_lst_tpl 
  

lst_tpl = [(), ('python', '12'), (), ('c', 'c++'), ('',''), ('php', 'c#', 17), ()] 

print(remove_empty_tuples(lst_tpl)) 



'''
run:

[('python', '12'), ('c', 'c++'), ('', ''), ('php', 'c#', 17)]

'''

 



answered Dec 17, 2019 by avibootz
0 votes
def remove_empty_tuples(lst_tpl): 
    new_lst_tpl = filter(None, lst_tpl) 
    return new_lst_tpl 
  

lst_tpl = [(), ('python', '12'), (), ('c', 'c++'), ('',''), ('php', 'c#', 17), ()] 

print(list(remove_empty_tuples(lst_tpl)))



'''
run:

[('python', '12'), ('c', 'c++'), ('', ''), ('php', 'c#', 17)]

'''

 



answered Dec 17, 2019 by avibootz
...