user1807338
user1807338

Reputation: 195

Is there way to remove element from list in Mathematica

Is there function in Wolfram Mathematica for removing element from original list? For example

a={1,2,3};
DeleteFrom[a,1];
a

a={2,3}

If it is absent can anyone give example of efficient variant of such function? (I know that there is function Delete() but it will create new list. This is not good if list is big)

Upvotes: 2

Views: 13218

Answers (6)

Anshul Dadwal
Anshul Dadwal

Reputation: 1

In Mathematica, you can drop the first and last elements from a list using the Most and Rest functions. Here's how you can do it:

originalList = {1, 2, 3, 4, 5}; droppedList = Most[Rest[originalList]];

In this example, Most is used to drop the last element (Rest would drop the first element), effectively removing both the first and last elements from the list originalList. The result will be stored in the variable droppedList.

Here's the result:

droppedList = {2, 3, 4}

Upvotes: 0

SnzFor16Min
SnzFor16Min

Reputation: 140

Both solutions from @user3446498 and @pmsoltani won't actually delete the element. Test:

a = {1, 2, 3};
a[[2]] = Sequence[]; (* or Nothing *)
--a[[1]];
++a[[2]];

a

They both output {0, 4, 3}, while {0, 4} is expected.

Replacing 2nd line with a = Delete[a, 2]; would work.

Upvotes: 0

pmsoltani
pmsoltani

Reputation: 1222

Improving the user3446498's answer, you can do the following:

In[1] := a = {1,2,3};

In[2] := a[[1]] = Nothing;

In[3] := a
Out[3] = {2,3}

In[4] := a == {2,3}
Out[4] = True

this Nothing symbol was introduced in in the 10th version (2015), see here.

Upvotes: 1

user3446498
user3446498

Reputation:

Assign the element to an empty sequence and it will removed from the list. This works for any element.

In[1] := a = {1,2,3}
Out[1]= {1,2,3}

In[2] := a[[1]] = Sequence[]
Out[2] = Sequence[]

In[3] := a
Out[3] = {2,3}

Yes Mathematica tends towards non-destructive programming, but the programmers at Wolfram are pretty clever folks and the code seems to run pretty fast. It hard to believe they would always copy a whole list to change one element, i.e. not make any optimizations.

Upvotes: 1

Mr.Wizard
Mr.Wizard

Reputation: 24336

Every time you change the length of a list in Mathematica you cause a reallocation of the list, which takes O(n) rather than O(1) time. Though no "DeleteFrom" function exists, if it did it would be no faster than a = Delete[a, x].

If you can create in advance a list of all the elements you wish to delete and delete them all at once you will get much better performance. If you cannot you will have to find another way to formulate your problem. I suggest you join us on the proper Stack Exchange site if you have additional questions:

enter image description here

Upvotes: 2

High Performance Mark
High Performance Mark

Reputation: 78316

If you want to drop the first element from a list a the statement

Drop[a,1]

returns a list the same as a without its first element. Note that this does not update a. To do that you could assign the result to a, eg

a = Drop[a,1]

Note that this is probably exactly what Delete is doing behind the scenes; first making a copy of a without its first element, then assigning the name a to that new list, then freeing the memory used by the old list.

Comparing destructive updates and non-destructive updates in Mathematica is quite complicated and can take one deep into the system's internals. You'll find a lot about the subject on the Stack Exchange Mathematica site.

Upvotes: 4

Related Questions