So let's say I have 10 lists named aj:
I can check which list is empty
if a.empty:
do something
But in what way can I print only the non empty lists:
for all lists in aj: print(non-empty lists)
try list comprehensions:
>>> list_of_lists = [[], [1], [], [2,3],[]]
>>> list_of_lists
[[], [1], [], [2, 3], []]
>>> [ l for l in list_of_lists if l]
[[1], [2, 3]]
because and empty list is not truthy , if l
evaluates to false when the list is empty, so it is not witheld.
have you tried just check for any element in the list?
for list_i in all_lists:
if list_i:
print(list_i)
let's suppose that you regrouped your 10 lists in a list L so L is now a list of your 10 lists,so this sample of code print the non empty lists which means those who have more than one element: for l in L: if (len(l)>0): print(l)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.