python - Removing empty Counter() objects from a list -


i have simple list of counters:

> counter_list counter({'pig': 1}), counter(), counter({'chicken': 3}) 

i cannot seem find simple way remove empty counter.

i have tried using

counter_list += counter() 

as per documentation, no luck.

del  

seems delete contents of counter, not actual counter, , @ best creates more empty counter.

any advice appreciated, , apologies in advance future forehead-slapper.

use list comprehension build new counter_list:

counter_list = [item item in counter_list if item] 

this works because empty counter falsish, while non-empty counter truish:

in [27]: bool(collections.counter()) out[27]: false  in [28]: bool(collections.counter([1])) out[28]: true 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -