Python. clean list if other list have empty sublists at same indices

Hello,

I do not know if I explain writting but I just want to get something like the screenshot with nodes but in python, basically remove from other list whatever index has empty list from a list of sublists.

Something like this example:

list1=[[[],[],["a","b","c"],[],["s","d","f"]],[[],[],["a","b","c"],[],["s","d","f"]]];
list2=[["slist1","slist2","slist3","slist4","slist5"],["slist1","slist2","slist3","slist4","slist5"]];
result=[["slist3","slist5"],["slist3","slist5"]]

clean list if other list have empty sublists at same indices.dyn (9.5 KB)

You need to zip the two lists together so that you can loop through them simultaneously. Then you can just check to see if the item in List1 is empty. If it’s empty, do nothing. If it’s not, add the respective List2 item to an output list.

no clue

Try doing some research on your own before asking people to give you the answer.

This thread has a ton of good resources for Python in general.

There are plenty of other threads that deal with list comprehension and zipping lists. The alternative is to use an index counter for the length of your lists and use that to manually coordinate pairs, but I think zipping is better practice.

This page gives a quick rundown of what list zipping is and how you can use it to pair list values.

1 Like

I think you could also use ennumurate function in a forloop to get the indices of all empty lists and then iterate through your other list with those indices to retrieve them.