7
$\begingroup$
a = Table[i, {i, 1, 10}]
Drop[a, {5, 8}]

If go with the above procedure, it will delete all the elements from between position 5 to 8. How can I drop elements which are at position 5 and position 8 using Drop?

$\endgroup$

4 Answers 4

13
$\begingroup$

Use instead Delete, in this way:

a = Table[i, {i, 1, 10}];
Delete[a, {{5}, {8}}]

(*

{1, 2, 3, 4, 6, 7, 9, 10}

*)

By the way, you can be far more efficient:

a = Range[10]
$\endgroup$
4
  • $\begingroup$ can we give the list of position which I wanted to drop like {3,5,8,9} $\endgroup$
    – acoustics
    Nov 29, 2018 at 7:49
  • 2
    $\begingroup$ Delete[a,{{3},{5},{8},{9}}] $\endgroup$ Nov 29, 2018 at 7:50
  • 2
    $\begingroup$ Also Delete[a, Partition[{3, 5, 8, 9}, 1]] comes to mind... if the list of positions gets longer. $\endgroup$ Nov 29, 2018 at 8:04
  • $\begingroup$ In general, you can use Delete[a, List/@{3, 5, 8, 9}]. $\endgroup$
    – Somos
    Jan 28 at 21:15
6
$\begingroup$
Drop[a, {5, 8, 3}]

{1, 2, 3, 4, 6, 7, 9, 10}

Fold[Drop[#, {#2}] &, a, Reverse[{5, 8}]]

{1, 2, 3, 4, 6, 7, 9, 10}

$\endgroup$
1
$\begingroup$
list = Range[10];

p = {5, 8};

Using SubsetReplace (new in 12.1)

SubsetReplace[list, p :> Nothing]

{1, 2, 3, 4, 6, 7, 9, 10}

$\endgroup$
1
$\begingroup$
list = Range[10];

Using SequenceCases:

p = {5, 8};

SequenceCases[list, s : {Except[Alternatives @@ p]} :> Splice@s]

(*{1, 2, 3, 4, 6, 7, 9, 10}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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