7
$\begingroup$

I will like to know if there is an equivalent to python's pop method, here is how it's done in Python:

>>> my_list = [1,2,3,4,5]
>>> my_list
[1, 2, 3, 4, 5]
>>> value = my_list.pop(0)
>>> value
1
>>> my_list
[2, 3, 4, 5]

With pop you get and delete an element from a list.

$\endgroup$
6
  • $\begingroup$ Try the commands First (to get the first element) and Rest (to get the remainder of the elements) $\endgroup$
    – bill s
    Jan 15, 2017 at 18:21
  • $\begingroup$ @bills and if I want to do pop(4)? The idea is right but i want to be able to grab any element on the list. $\endgroup$ Jan 15, 2017 at 18:24
  • $\begingroup$ Then use Part: list[[1;;4]] and list[[5;;All]] $\endgroup$
    – bill s
    Jan 15, 2017 at 18:26
  • 9
    $\begingroup$ Why are you looking for an equivalent? In Mathematica it is generally not a good idea to mutate data structures. There is no equivalent because such a function (that both modifies its argument and returns something) really has no place in a language like Mathematica. Are you looking for an efficient stack data structure? If so, consider a nested list like {3, {2, {1, {}}}}. Use First and Rest to pop an item from it. $\endgroup$
    – Szabolcs
    Jan 15, 2017 at 18:42
  • 3
    $\begingroup$ pop is like Part[ ] but with an additional side effect. In Mathematica we normally try to avoid side effects. However we have functions like AppendTo that work like that. $\endgroup$ Jan 15, 2017 at 18:44

4 Answers 4

7
$\begingroup$

There is no direct equivalent. You could use something like this:

SetAttributes[pop, HoldFirst]
pop[list_, n_] := With[{item = list[[n]]}, list = Delete[list, n]; item]

mylist = {1, 2, 3, 4, 5};

pop[mylist, 4]
(* 4 *)

mylist
(* {1, 2, 3, 5} *)
$\endgroup$
3
  • $\begingroup$ See @JoseLuisMartin 's answer. Surprisingly there is an equivalent. $\endgroup$
    – QuantumDot
    Jan 17, 2017 at 15:05
  • $\begingroup$ @QuantumDot I wouldn't say that's equivalent - pop alters the list whereas TakeDrop returns a new list. $\endgroup$ Jan 17, 2017 at 18:44
  • $\begingroup$ Ah yes, indeed. $\endgroup$
    – QuantumDot
    Jan 17, 2017 at 21:00
9
$\begingroup$

TakeDrop[list,seq]

list = {1, 2, 2, 4, 5, 6};
seq  = {2, 3};
pair = TakeDrop[list, seq] (* {{2, 2}, {1, 4, 5, 6}} *)
list = pair[[2]]           (* {1, 4, 5, 6} *)
pop = pair[[1]]            (* {2,2} *)
$\endgroup$
6
$\begingroup$

Here is a simple way that I think mimics the Python script well.

myList = Range[5];
{value, myList} = {#, {##2}} & @@ myList;
Column[{value, myList}]

results

$\endgroup$
1
$\begingroup$

Introduced in v10, you can use Nothing:

mylist = {1, 2, 3, 4, 5};

(* save the particular item, if you need it *)
item = mylist[[1]];

mylist[[1]] = Nothing;
(* mylist = {2, 3, 4, 5} *)

mylist == {2, 3, 4, 5}
(* True *)

mylist === {2, 3, 4, 5}
(* True *)

More info here.

$\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.