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:
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
else:
pass
if hasMatch:
appendList = insertMe + lst
newList.append(appendList)
else:
newList.append(lst)
OUT = newList
@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.
Itās not the number of lines which count, but the speed at which the overall code executes.
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