Suppose I have the following list:


and I have a given value of maxDh (let's say 0.5) . How can I replace only the values in the second position with a given operation (e.g. maxDh*5) only when the values of the first position are greater than 74 and maxDh is lower than 1.2 (as in this case)?. For example given that in this example maxDh=0.5, then I would get: {{40,0.0712996},{40.,0.0712996},{40.,0.0712996}......{74.0202,2.5},{74.0404,2.5},{74.2023,2.5}....etc.

Thank you very much in advanced,

  • $\begingroup$ Perhaps list /. {a_?(# > 74 &), b_?(# < 1.2 &)} :> {a, 2.5} ? Can you explain what dhMax is here though? Is it the second element of each pair? $\endgroup$
    – MarcoB
    Commented May 24, 2020 at 0:52
  • $\begingroup$ Looks good, comparing ListPlot[%] before & after $\endgroup$
    – prog9910
    Commented May 24, 2020 at 0:59
  • $\begingroup$ @MarcoB Thank you!. In this example maxDh (which I corrected in the EDIT) is the number that tells me if the condition should be done or not. Anytime that maxDh is lower than 1.2, then I need to replace all of the values in the second position with the given operation only when the values of the first postion are greater than 74. So, In the case of your code perhaps it would be something like: list /. {a_?(# > 74 &), b_?(maxDh < 1.2 &)} :> {a, 2.5}? but it does not seem to work like this $\endgroup$
    – John
    Commented May 24, 2020 at 1:04
  • $\begingroup$ Also, it is worth mentioning that maxDh has nothing to do with the list. It's just a number I use in my code that tells me if I should apply the requested code or not. If it is below 1.2 then I applied the requested code with the condition, if It is greater than 1.2, then simply the list remains the same and nothing happens. $\endgroup$
    – John
    Commented May 24, 2020 at 1:10

1 Answer 1


Is this what you need?

list = ToExpression@Import["https://pastebin.com/raw/zFin7kkB"];

conditionalreplace[list_List, maxDh_: 0.5, threshold_: 1.2] := 
   maxDh < threshold,
   list /. {a_?(# > 74 &), b_} :> {a, 5 maxDh}, 

ListLinePlot[{list, conditionalreplace[list]}]


  • $\begingroup$ yes! Thank you very much ! $\endgroup$
    – John
    Commented May 24, 2020 at 1:48
  • $\begingroup$ MarcoB, I have posted a similar question but with some modification here: mathematica.stackexchange.com/questions/222505/… . Perhaps you can help me there too as you are already familiar with the problem. Thanks ! $\endgroup$
    – John
    Commented May 24, 2020 at 2:29

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.