6
$\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 to drop elements which are a position 5 and position 8 using drop function.

$\endgroup$

2 Answers 2

9
$\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$
3
  • $\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
5
$\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$

Your Answer

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

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