Python. Remove items of the sublists if they exceed a number of items

Hello,

I am trying to remove specific items at indexes if the sublists are greater than the minimum number of items of all sublists. I show the example in OOTB nodes and I would like to achieve same result using python node of Dynamo.

For example: if the count of items in sublists is 41 but the mimimum found is 40, then remove the difference of those 2 in number of items, so remove 1 item at the specified index, in this case 26. If the difference is 2 then remove index 26 and 27, if the difference is zero then remove nothing.


removeitematindexifdifferencecountsublists.dyn (15.2 KB)

These look promising:

Something to try

lengths = [ len(lst) for lst in listOfLists ]
min = min(lengths) 
newLists = [lst[:min] for lst in listOfLists ]

I think this is removing the last items of the list that exceed but I would like to remove specific indexes in the sublists if sublist exceeding

Ah. Use the del function of lists instead.

Since I’m guessing you’ll eventually run into a “I want to delete only one this time” or “only two” that time it’s best to move into a if : else statement now.

For a list of[ [ 1,2,3,4,5], [1,2,3], [1,2,3,4] ] this will return [ [1,2,5], [1,2,3], [1,2,4] ]

for lst in listOfLists:
	if len(lst) > 4:
		del lst[2:4]
	elif len(lst) > 3:
		del lst[2]
1 Like

interesting, so what would be the output name here? the same than the input: listOfLists?

Yes. Delete just removes the data for good. Considering the insane size of some of the lists I’ve seen you working with, this is likely better than continuing to pack stuff into memory. Otherwise Python won’t give you the speed gains you’re after.

2 Likes

I agree I need python because OOTB nodes kill the computer and do not finish the work.

In this case I do not know if the length of the sublist needs to be greater than a specific number, so I would have to ask beforehand to all sublists which one has the minimum number of items and use this number in the conditional if elif

Not necessarily. Personally I find that if you write the automation in another way you can circumvent the issue in either Dynamo or Python; but without the full context we can’t really help there. May be best to look into a Python class, or reach out to a consultant to help square you away, as you’re trying to learn how to write the code against the full data set rather than a partial one.

I just want to express some caution, as declaring 100’s of lists of variables that are hundreds of thousands of items long is going to be slower than iterating over a list 100 times. Packing everything in memory time and time again is going to lead you back to the same problem you had before, though perhaps at a different location in the code base.