3
$\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$
5
$\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$
  • $\begingroup$ can we give the list of position which I wanted to drop like {3,5,8,9} $\endgroup$ – acoustics Nov 29 '18 at 7:49
  • 2
    $\begingroup$ Delete[a,{{3},{5},{8},{9}}] $\endgroup$ – David G. Stork Nov 29 '18 at 7:50
  • 1
    $\begingroup$ Also Delete[a, Partition[{3, 5, 8, 9}, 1]] comes to mind... if the list of positions gets longer. $\endgroup$ – Henrik Schumacher Nov 29 '18 at 8:04
3
$\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, privacy policy and cookie policy

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