I have two lists, list 1 and list 2…list 2 has 2 sublists…i want to merge sublist in list 2 with list 1 using python.When merging sublist its index should be a continuation of list 1 i.e 12…basically i need 2 list as output , one with 18 elements and other with 16 elements…Can anybody suggest python script for the above.
No need for python - the out of the box List.Join node would work. You may want to add a List.Flatten as well, depending on the desired output.
If you wanted to go with Python the append function would do the trick, but you will need to iterate over your list correctly.
I want to do that inside one python script…so out of the box list.join won’t help…also append will add the second list as a sublist…not as a continuation.As per my understanding list.extend should work …but when using extend i am getting an error which says listobject has no attribute extend.
On my phone so I can’t test the stuff below, but as I stated append will work if you iterate over the sublists correctly.
listA = [1,2,3]
listB = [ [4,5,6] , [7,8,9] ]
option1 = [ listA.Append(i) for sl in listB for i in sl ] #my list comprehension for statements may be backwards
option2 = listA
for sl in listB:
[ option2.Append(i) for i in sl ]
Edit: could also join the lists and flatten the joined list… plenty of examples on the web for this, one such promising set of solutions can be found here: Python: How to Flatten a List of Lists
Thanks Option 1 worked.