Python. Insert a list in beggining of sublists if sublists first item contains a word


I would like to do that in python as this task could consume quite a lot of memory RAM to process: to insert a list at the beggining of sublists which first item contains a specific word, example:

I used if node in a custom package @GavinCrump

You’ve been posting a lot of ‘solve my python challenge’ threads lately, just wanted to check that you’re in the process of learning Python from the fundamentals up at least? It’s good to explore challenges like these, but some of these probably seem like they’re being posted before an attempt has been made.

I point this out only in the hope of helping guide your learning on a more effective pathway.

In the interest of being helpful anyway, here’s a solution. Noting that many concepts are quite different to how nodes in Dynamo work. Fundamental concepts like joining lists with a + and using ‘in’ statements to see if strings contain a substring are Python fundamentals so handy to learn ground up.

checkLists = IN[0]
matchMe    = IN[1]
insertMe   = IN[2]

newList = []

for lst in checkLists:
	hasMatch = False
	for obj in lst:
		if matchMe in obj:
			hasMatch = True
	if hasMatch:
		appendList = insertMe + lst

OUT = newList

1 Like

@ruben.romero You can do it with startswith & list comprehension as below:

Note: It will fail if there is an empty sublist


@AmolShah @GavinCrump Thanks very much for your help, I hope it is useful for the community for future if needed.

I am forced to learn Python because a script originally done with nodes, it crashes literally the computer and switches off and with python can be simplified quite a lot, and I am not a programmer.

My question as others are quite basic but the final script is more complex, so having simple examples I can try to develop more by myself.

The question is looking to achieve the following challenge: Python. Add a sublist that starts with a word to the following sublists and repeat from each time a sublist starts with word appears

Trumped by a one liner again, sigh.

Think I’m going to take a break for a bit from the forums.


It’s not the number of lines which count, but the speed at which the overall code executes. :slight_smile:

Particularly in instances like is described here, as the process of packing say 10,000 elements into a list, then iterating over that, thereby generating sublist after sublist a few hundred times may actually be exponentially slower than processing one element a few hundred times and then discarding the memory used or overwriting it in one for loop.

We can’t give “here is the best solution” as we’re only seeing parts of the puzzle, which is fine as these threads are leading to a good set of collective knowledge.


If the primary goal is to execute code, sure. Generally I think it’s better to expand the logic when solving challenges in Python, then form the oneliners later once you grasp the expanded solution to the problem. I nearly always work this way, and sometimes avoid oneliners just so my code is readable by others.

People don’t learn ground up if all they’re doing is throwing oneliners into python nodes, the former of which which to me is the goal of a forum such as this - it’s something I’ve noticed an increasing tendency for in solutions being given by people (not necessarily that often here, but stack OF and out in practice I see some ghastly oneliners left behind by leavers).

I don’t mind not being given the solution too (I can see it probably sounded that way), I’m just genuinely concerned that often the pursuit of threads is less ‘I’d like to know how to do X’ and instead is ‘give me some thing I can hit run on, and I’ll move on’.


Thanks @GavinCrump it worked well, easy to read and understand and possibly modify in the future because it is read like step by step, but if I do not care the script but the quickest solution hehe. If you feel better I remove the solution to this post because any solution is good here