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

I get the $n$ Weather stations closer 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 in each station and save it in a list with the date in which this 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. After that I check the length of temperatures list and 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:


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

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

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
    Nov 25 '14 at 0:58

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.