I have have list of lists and I would like to be able to append an element to the start of each list. I am sure there must be an elegant solution.

EG: a and {{b,c},{d,e,f}} -> {{a,b,c},{a,d,e,f}}

If I want to apply a function to a list of lists I normally use @@@ however I don't know how to modify this so that it gets feed into the second argument or AppendTo.

Can someone please explain how I could achieve this?

  • 1
    $\begingroup$ Prepend[#, a] & /@ {{b, c}, {d, e, f}}? I feel there is a thread on this around already, probably with a thousand nice solutions... $\endgroup$ – Yves Klett Sep 11 '14 at 9:45
  • $\begingroup$ Yep, thanks. I am bad with # and &. Cheers! $\endgroup$ – User Sep 11 '14 at 9:47
  • $\begingroup$ This is closely related Interlacing a single number into a long list, however I'm sure there are exact duplicates of this question. $\endgroup$ – Artes Sep 11 '14 at 10:01
  • $\begingroup$ You're probably right, I had a search but did not find it, which probably says more about my searching skills. $\endgroup$ – User Sep 11 '14 at 10:13
  • $\begingroup$ I have marked this as "already has an answer" -- please see the link inserted at the top of the Question. Although the examples there show a rectangular array many of the methods work on a ragged array as well. Examples: Prepend[#, 0]& /@ lists and {0, ##} & @@@ lists $\endgroup$ – Mr.Wizard Sep 11 '14 at 10:14

Could be done with:

l = {{b, c}, {d, e, f}};
Join[{a}, #] & /@ l
{{a, b, c}, {a, d, e, f}}
| improve this answer | |
{a, ##} & @@@ {{b, c}, {d, e, f}}
(* {{a, b, c}, {a, d, e, f}}  *)
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.