3
$\begingroup$

I would need a function that distributes a nested list in a specified position.

A sample of this function could be:

try={{1,{a,b,c},2},3,4,{5,6}} DistributeNested[try,{1,2}] 

which output would be

{{{1,a,2},3,4,{5,6}},{{1,b,2},3,4,{5,6}},{{1,c,2},3,4,{5,6}}} 

How can I realise such a function?

$\endgroup$
2
  • $\begingroup$ What have you tried. You will not learn Mathematica by having others do the work for you. $\endgroup$ Commented Mar 18, 2021 at 22:27
  • 1
    $\begingroup$ @ciao I have a function that creates as many copies of the list as the number of elements in the nested list and assigns to each of them at chosen position one of the elements. But my function is slow because I need to work with bigger lists than the one shown in the example and I was wondering if there is a smarter/quicker way of doing it. On the other hand, I am not asking for advice on how to learn Mathematica quicker. $\endgroup$ Commented Mar 18, 2021 at 22:34

1 Answer 1

3
$\begingroup$
Thread[MapAt[Thread, try, {1}], List, 1] 
{{{1, a, 2}, 3, 4, {5, 6}}, {{1, b, 2}, 3, 4, {5, 6}}, {{1, c, 2}, 3, 4, {5, 6}}} 

Update: Using the second and third arguments of Thread you can specify the heads and positions of arguments to thread over:

try2 = {{1, {a, b, c}, {e, f, g}}, 3, 4, {5, 6}}; Thread[MapAt[Thread, try2, {1}], List, 1] 
{{{1, a, e}, 3, 4, {5, 6}}, {{1, b, f}, 3, 4, {5, 6}}, {{1, c, g}, 3, 4, {5, 6}}} 
Thread[MapAt[Thread[#, List, {2}] &, try2, {1}], List, 1] 
{{{1, a, {e, f, g}}, 3, 4, {5, 6}}, {{1, b, {e, f, g}}, 3, 4, {5, 6}}, {{1, c, {e, f, g}}, 3, 4, {5, 6}}} 
Thread[MapAt[Thread[#, List, {3}] &, try2, {1}], List, 1] 
{{{1, {a, b, c}, e}, 3, 4, {5, 6}}, {{1, {a, b, c}, f}, 3, 4, {5, 6}}, {{1, {a, b, c}, g}, 3, 4, {5, 6}}} 
try3 = {{1, foo[a, b, c], bar[e, f, g]}, 3, 4, {5, 6}} ; Thread[MapAt[Thread[#, foo] &, try3, {1}], List, 1] 
{foo[{1, a, bar[e, f, g]}, {1, b, bar[e, f, g]}, {1, c, bar[e, f, g]}], 3, 4, {5, 6}} 
Thread[MapAt[Thread[#, bar] &, try3, {1}], List, 1] 
{bar[{1, foo[a, b, c], e}, {1, foo[a, b, c], f}, {1, foo[a, b, c], g}], 3, 4, {5, 6}} 
Thread[MapAt[Thread[#, foo] &, try3, {1}], foo, 1] 
foo[{{1, a, bar[e, f, g]}, 3, 4, {5, 6}}, {{1, b, bar[e, f, g]}, 3, 4, {5, 6}}, {{1, c, bar[e, f, g]}, 3, 4, {5, 6}}] 
$\endgroup$
3
  • $\begingroup$ @kgir the function is almost what I am looking for. But if you consider try={{1, {a, b, c}, {e, f, g}}, 3, 4, {5, 6}} and Thread[MapAt[Thread, try, {1}], List, 1] then the output would be {{{1, a, e}, 3, 4, {5, 6}}, {{1, b, f}, 3, 4, {5, 6}}, {{1, c, g}, 3, 4, {5, 6}}} not {{{1, a, {e, f, g}}, 3, 4, {5, 6}}, {{1, b, {e, f, g}}, 3, 4, {5, 6}}, {{1, c, {e, f, g}}, 3, 4, {5, 6}}} $\endgroup$ Commented Mar 19, 2021 at 9:39
  • $\begingroup$ @Stefano, please see the update. $\endgroup$ Commented Mar 19, 2021 at 15:27
  • $\begingroup$ @kgir thank you! $\endgroup$ Commented Mar 19, 2021 at 23:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.