I am updating a legacy Python script, and was wondering if it is possible to unwrap a list from inside another list, or more generally to unwrap a list into just its elements.
My specific problem is that I have a large list, and now am being passed another list to place in the middle of this list, instead of a single element, i.e. I have a list of the form [1,2,3,’passed parameter’,4,5], and I am now being passed a list of the form [‘list’,’of’,’parameters’], with the goal of generating the list [1,2,3,’list’,’of’,’parameters’,4,5].
The obvious solution to this is to write [1,2,3] + [‘list’,’of’,’parameters’] + [4,5] but I am worried that this will iterate over [‘list’,’of’,’parameters’] and [4,5], instead of just the [‘list’,’of’,’parameters’]. (This matters because apart from being in a performance critical part of the program, the elements of the list are expensive to evaluate.)
It’s not entirely clear what you mean – how are you determining where to place the list? – but you can use slicing to do this.