Skip to main content
added 260 characters in body
Source Link
Chris Degnen
  • 31.3k
  • 2
  • 56
  • 109
list = {1, 4, 1, 2, 3, 5};
nonopt = {1, 3};

This gives a sorted output:

Complement[list, nonopt]
{2, 4, 5}

To preserve the order, this is a simple method.

DeleteCases[list, Alternatives @@ nonopt]
{4, 2, 5}

Edit

For list with lists, matching on the first element:

list = {{1, a}, {4, d}, {1, x}, {2, b}, {3, c}, {2, y}, {5, e}};
nonopt = {1, 3};

DeleteCases[list, {Alternatives @@ nonopt, __}]
{{4, d}, {2, b}, {2, y}, {5, e}}
list = {1, 4, 1, 2, 3, 5};
nonopt = {1, 3};

This gives a sorted output:

Complement[list, nonopt]
{2, 4, 5}

To preserve the order, this is a simple method.

DeleteCases[list, Alternatives @@ nonopt]
{4, 2, 5}
list = {1, 4, 1, 2, 3, 5};
nonopt = {1, 3};

This gives a sorted output:

Complement[list, nonopt]
{2, 4, 5}

To preserve the order, this is a simple method.

DeleteCases[list, Alternatives @@ nonopt]
{4, 2, 5}

Edit

For list with lists, matching on the first element:

list = {{1, a}, {4, d}, {1, x}, {2, b}, {3, c}, {2, y}, {5, e}};
nonopt = {1, 3};

DeleteCases[list, {Alternatives @@ nonopt, __}]
{{4, d}, {2, b}, {2, y}, {5, e}}
added 8 characters in body
Source Link
xyz
  • 655
  • 4
  • 38
  • 117
list = {1, 4, 1, 2, 3, 5};
nonopt = {1, 3};

This gives a sorted output:

Complement[list, nonopt]

{2, 4, 5}

{2, 4, 5}

To preserve the order, this is a simple method.

DeleteCases[list, Alternatives @@ nonopt]

{4, 2, 5}

{4, 2, 5}
list = {1, 4, 1, 2, 3, 5};
nonopt = {1, 3};

This gives a sorted output:

Complement[list, nonopt]

{2, 4, 5}

To preserve the order, this is a simple method.

DeleteCases[list, Alternatives @@ nonopt]

{4, 2, 5}

list = {1, 4, 1, 2, 3, 5};
nonopt = {1, 3};

This gives a sorted output:

Complement[list, nonopt]
{2, 4, 5}

To preserve the order, this is a simple method.

DeleteCases[list, Alternatives @@ nonopt]
{4, 2, 5}
Source Link
Chris Degnen
  • 31.3k
  • 2
  • 56
  • 109

list = {1, 4, 1, 2, 3, 5};
nonopt = {1, 3};

This gives a sorted output:

Complement[list, nonopt]

{2, 4, 5}

To preserve the order, this is a simple method.

DeleteCases[list, Alternatives @@ nonopt]

{4, 2, 5}