I'm trying random things in Mathematica just for learning and I can't solve this problem. I want to use the Delete function to delete some elements in a list but it's not working. My program consists of this:

I get the $n$ Weather stations closest to Chicago:

stations = WeatherData[{"Chicago", 20}]

{"C3175", "KCGX", "C8163", "D3691", "KMDW", "D1866", "AN168", "AS937", "AN169", "C2741", "C5827", "C9647", "D2206", "AP878", "C3332", "C5247", "C8740", "C6091", "AT367", "AR820"}

Then, I make a list with the temperature at each station and save it in a list with the date at which the temperature was taken:

temperatures = {DateObject[WeatherData[stations[[#]], "Temperature","DateValue"][[1, 1]]],WeatherData[stations[[#]], "Temperature"]} & /@ Range[Length[stations]]

{{DateObject[{2014, 11, 24}], Quantity[-1., "DegreesCelsius"]}, {DateObject[{2008, 6, 18}], Quantity[14., "DegreesCelsius"]}, {DateObject[{2008, 3, 28}], Quantity[2.8, "DegreesCelsius"]}, {DateObject[{2013, 3, 8}], Quantity[0.6, "DegreesCelsius"]}, {DateObject[{2014, 11, 24}], Quantity[-1., "DegreesCelsius"]}, {DateObject[{2014, 11, 24}], Quantity[-2.1, "DegreesCelsius"]}, {DateObject[{2011, 12, 28}], Missing["NotAvailable"]}, {DateObject[{2014, 11, 24}], Quantity[-1., "DegreesCelsius"]}, {DateObject[{2012, 12, 18}], Missing["NotAvailable"]}, {DateObject[{2012, 1, 9}], Missing["NotAvailable"]}, {DateObject[{2011, 11, 8}], Quantity[-17.7, "DegreesCelsius"]}, {DateObject[{2010, 10, 30}], Quantity[14.4, "DegreesCelsius"]}, {DateObject[{2012, 8, 4}], Quantity[27.2, "DegreesCelsius"]}, {DateObject[{2014, 11, 24}], Quantity[-2.1, "DegreesCelsius"]}, {DateObject[{2011, 1, 28}], Missing["NotAvailable"]}, {DateObject[{2008, 5, 24}], Quantity[8.3, "DegreesCelsius"]}, {DateObject[{2008, 3, 10}], Quantity[-1., "DegreesCelsius"]}, {DateObject[{2011, 5, 20}], Quantity[21.7, "DegreesCelsius"]}, {DateObject[{2010, 11, 25}], Quantity[4.4, "DegreesCelsius"]}, {DateObject[{2014, 11, 24}], Quantity[-2.7, "DegreesCelsius"]}}

Now I try to delete every temperature that is not from today:

If[temperatures[[#, 1]] == Today, Print["Correct"],Delete[temperatures, #]] & /@ Range[Length[temperatures]]

But this is not working. When I check the length of the temperatures list, it's still 20. Indeed, I've tried:

temperatures[[#, 1]] == Today & /@ Range[Length[temperatures]]

{True, False, False, False, True, True, False, True, False, False, False, False, False, True, False, False, False, False, False, True}

So the condition is fine. Even trying this:

Delete[temperatures, 1]

The length of temperatures is still 20. Why is this not working with the function Delete?

  • $\begingroup$ I recommend looking into the Select and Cases functions. $\endgroup$
    – wxffles
    Commented Nov 25, 2014 at 1:06

2 Answers 2


Delete does not modify the object itself the element being deleted from.

x = {a, b, c, d};
Delete[x, 3]

x is still not changed now after the call to Delete. What you get back is the result of the deletion. To actually change x you need to assign this result back to the original object to change it:

x = Delete[x, 3]

Only now will x become

Mathematica graphics

Mathematica is not like Fortran, it is not a call by reference.

  • $\begingroup$ Thank you. Now I can see that was an stupid question. $\endgroup$
    – Danowsky
    Commented Nov 25, 2014 at 0:58
x = {a, b, c, d};


Using ApplyTo (new in 12.2)

x //= Delete[3];


{a, b, d}


Using Nothing (new in 10.2)

x = {a, b, c, d};

x[[3]] = Nothing;


{a, b, d}


Passing x by reference

SetAttributes[del, HoldAll]

del[x_, p_] := x = Delete[x, p]

x = {a, b, c, d};

del[x, 3];


{a, b, d}


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.