Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Consider a big list where all the inner lists length's are the same

 list = {{x1, y1, z1, t1}, {x2, y2, z2, t2}, {x3, y3, z3, t3}, ...};
 elementNo = {4, 3, 2};

I want output like this,

{{x1}, {x2}, {x3}, ...}

That is, when I specify elementNo as {4, 3, 2}, I mean the elements at positions 2, 3 and 4 in the sub-lists should be removed -- I want the elements at position 1 only. When I specify elementNo as {2, 3}, I want

 {{x1, t1}, {x2, t2}, {x3, t3}, ...}

How can I do this?

share|improve this question
Hm... okay, I guess I see now. You should describe this problem as deleting elements, as that is what it is. – Mr.Wizard Mar 18 '13 at 6:07
@Mr.Wizard I tried for getting that output,but I didn't get it. – subbu Mar 18 '13 at 6:10
@Mr.Wizard we have to remove the specific position elements from the main list,that specific element position getting from elementNO. – subbu Mar 18 '13 at 6:12
I answered this. Please take the time to Accept some of the answers to your earlier questions. – Mr.Wizard Mar 18 '13 at 6:15
@m_goldberg You're obsessive about your edits, aren't you? – Mr.Wizard Mar 18 '13 at 6:52

Another option:

f[list_, pos_] := Module[{x = list},
  x[[All, pos]] = Sequence[];
share|improve this answer
+1 Short, sweet, and fast. – m_goldberg Mar 18 '13 at 13:07
f1[list_, spec_] := With[{sp = List /@ spec}, Delete[#, sp] & /@ list]

f1[list, {4, 3, 2}]
f1[list, {2, 3}]
{{x1}, {x2}, {x3}}
{{x1, t1}, {x2, t2}, {x3, t3}}
f2[list_, spec_] := list[[ All, Complement[Range@Length@First@list, spec] ]]

f2[list, {4, 3, 2}]
f2[list, {2, 3}]
{{x1}, {x2}, {x3}}
{{x1, t1}, {x2, t2}, {x3, t3}}
share|improve this answer
+1. Note that a more efficient form of List /@ specis Transpose[{spec}]. – Leonid Shifrin Mar 18 '13 at 8:33
@Leonid True, however I thought clarity might be better for this answer, and f2 is the higher performance method anyway. – Mr.Wizard Mar 18 '13 at 10:37
Ok, makes sense. – Leonid Shifrin Mar 18 '13 at 11:11

Here's another alternative:

f[list_, pos_] := With[{p = pos ~Sort~ Greater}, Fold[# ~Drop~ {#2} &, #, p] & /@ list]

f[list, {2, 3}]
(* {{x1, t1}, {x2, t2}, {x3, t3}} *)

If you can guarantee that the positions to be dropped will be input in descending order, you can drop the sorting step.

share|improve this answer

Try this one

list = RandomInteger[50, {10, 4}]
new = {};
elemNo = {4, 3, 2};
new = Map[Delete[#, {{elemNo[[1]]}, {elemNo[[2]]}, {elemNo[[3]]}}] &,list]
share|improve this answer
That won't work if the elemNo list is a different length. It's also not efficiently written, as you could much more easily do: Map[Delete[#, List /@ elemNo] &, list]. This is what I did in my answer for the function f1 but with the additional optimization of doing the List /@ elemNo once and inserting it using With. – Mr.Wizard Mar 18 '13 at 6:49

here is the way I would do it using my Excel mindset,

list = {{x1, y1, z1, t1}, {x2, y2, z2, t2}, {x3, y3, z3, t3}};
elementNo = {4, 3, 2};
todelete = {#} & /@ Sort@elementNo
tokeep = Delete[Range@Length@First@list, todelete]
remaining = Take[list, All, tokeep]

or something similar.

share|improve this answer

Another way, using Outer and Extract:

data = {{x1, y1, z1, t1}, {x2, y2, z2, t2}, {x3, y3, z3, t3}};
unwanted1 = {4, 3, 2};
unwanted2 = {2, 3};

extractComplement[data_List, spec_List] :=
  Module[{dataSize, subSize, survivors},
    dataSize = Length@data;
    subSize = Length@data[[1]];
    survivors = Outer[List, Range@dataSize, Complement[Range@subSize, spec]];
    Extract[data, #] & /@ survivors]

extractComplement[data, unwanted1]

{{x1}, {x2}, {x3}}

extractComplement[data, unwanted2]

{x1, t1}, {x2, t2}, {x3, t3}

share|improve this answer

If all sublists are indeed of the same length, use Transpose:

Transpose@Delete[Transpose@list, List /@ elementNo]
{{x1}, {x2}, {x3}}

Note that you can use the shorthand notation for Transpose, which makes the code really short:

Mathematica graphics

With other elementNo:

elementNo = {3, 2};
Transpose@Delete[Transpose@list, List /@ elementNo]
{{x1, t1}, {x2, t2}, {x3, t3}}
share|improve this answer
While you're using Transpose how about Delete[list\[Transpose], {elementNo}\[Transpose]]\[Transpose]? Looks great in the Notebook. :-) – Mr.Wizard Mar 18 '13 at 10:35
@Mr.Wizard I've set my PerformanceGoal to "Speed" instead of "CodeGolf". An extra Transpose might be just too much for a modern computer... :) – István Zachar Mar 18 '13 at 10:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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