Python Searching each item of list in other sub-lists

please i am very new in python, and i need to make a script for cable routing with cable numbers and cable tray names where the operation needed is on lists but am not able to do it.
if i have 2 lists like this: (the second list contain sub-lists)
List1 (which is the cables numbers)= (1, 2, 3, 4, 5)
List2 (Cable Tray names with cables inside) = ( [A1, 1, 2, 3] , [A3, 2, 3] , [A2, 1] , [A4, 4, 5] )
it is more clear in the image below

so i need to make a new list contains the cable number and all its related cable trays names which means that i want to take the first item from List1 which is (1) and search in all the sublist items of List2 and if found, then the output will be this item from list1 (1) and the first item from each sublist contains this item in List2 (A1, A2)

for example the output will be list with 5 sublists ( number of sublist = the number of items in the list1) as follow:
List3 (Routing) = ( [1] , [A1, A2] )
( [2] , [A1, A3] )
( [3] , [A1, A3] )
( [4] , [A4] )
( [5] , [A4] )

Can you please help me and sorry for the brackets () any suggestions for the python script?.
Thank you so much

Why use python for that?

of course if this can be done without python, and only with dynamo nodes, this will be very great
i thought that this can be done only in python, so can u please guide me or tell me how to achieve it ?

Just to clarify:
You have a List2 that contains A1, 1, 2 and 3.
What is β€œA1” and what is β€œ1”, β€œ2”, etc in the sublists?

I would guess you combined lists to get List2 because it’s kind of a weird combination, but you’d actually be better off keeping the cable tray names separate from the cable numbers. Then you could just compare your List1 of numbers with the numbers of List2 and use that to filter the names.

1 Like

list_num, list_tray=IN
z=[]
for element in list_num:
	i=list_num.index(element)
	z.append([element])
	for el in list_tray:
		if element in el:
			z[i].insert(i+1,el[0])
	
OUT= z

it works!! :smiley: thank you soo much, I just need to modify something as I think I use different version, like adding some flattens but it finally works.
And by they way, your guess is correct, I have already combined the parameters values in the cable tray at List2
Thank you for your help brother.

1 Like

It works also, the python script is great :smiley: merci Deniz
thank you so much guys it now works with dynamo or python :+1:

2 Likes