# Replace last element of a list if there is somewhere a 0 in the list

I have the following list of lists:

l = {{5,6,0,8,5,9}, {8,4,4,3,5,8}, {9,4,3,2,5,7}}


now if there is a 0 in one of the lists Mathematica should replace the last element in the list with 0 like this:

l1 = {{5,6,0,8,5,0}, {8,4,4,3,5,8}, {9,4,3,2,5,7}}


the l1 should be my output. I have no idea how is can do this, I hope someone can help.

• l /. x_ /; MemberQ[x, 0] :> ReplacePart[x, -1 -> 0] Jul 28, 2016 at 18:02
• @JasonB Dang it! I was 15 seconds too late. -- Here's an alternative: l1 = l /. a: {___, 0, ___ } :> ReplacePart[a, -1 -> 0] Jul 28, 2016 at 18:03
• ReplacePart is not necessary l1 /. {a___, 0, b___, _} -> {a, 0, b, 0}. I see no reason for RuleDelayed either? Jul 28, 2016 at 18:25
• @Coolwater RuleDelayed is standard if you are using variables in both sides of Rule; it prevents naming conflicts, etc. For instance, a = 3; {1, 2, 3} /. a_Integer -> a does not work. One could clear variables prior to running ReplaceAll, but writing Clear every time there is /. is too inconvenient! Jul 28, 2016 at 19:00
• @Coolwater Nice method, but heed JHM's warning. Jul 28, 2016 at 19:03

l[[All, -1]] = l[[All, -1]] Times @@@ Unitize@l ; l


or, better (thanks: @Mr.Wizard):

l[[All, -1]] *= Times @@@ Unitize@l ; l


{{5, 6, 0, 8, 5, 0}, {8, 4, 4, 3, 5, 8}, {9, 4, 3, 2, 5, 7}}

Update: Per @JasonB's comment, if you want to create a new list without changing l, you can use

Module[{l2 = #}, l2[[All, -1]] *= Times @@@ Unitize@l2 ; l2] &@l


or

ReplacePart[l, { i_, -1} :> Times @@ Unitize@l[[i]]]


to get

{{5, 6, 0, 8, 5, 0}, {8, 4, 4, 3, 5, 8}, {9, 4, 3, 2, 5, 7}}

• Using *= is shorter: l[[All, -1]] *= Times @@@ Unitize@l; l Jul 28, 2016 at 18:57
• @Mr.W, yes, thank you!
– kglr
Jul 28, 2016 at 18:58
• Nice! If you want to do this without changing the definition of the original list, ReplacePart[#, -1 -> Times @@ Unitize@#] & /@ l Jul 28, 2016 at 19:40
• This one's elegant (+1), but (possibly) trails slightly behind ciao's method since IIRC, MMA doesn't have an efficient built-in for the product of a list of numbers in the same manner that it does for the sum (Total). Jul 29, 2016 at 7:36

S/B quick (assuming non-negative list members as in OP):

Module[{ll = #}, ll[[Pick[Range@Length@ll, Min /@ ll, 0], -1]] = 0; ll] &


If negative members allowed, small performance impact:

Module[{ll = #}, ll[[Pick[Range@Length@ll, Min /@ Abs[ll], 0], -1]] = 0; ll] &

• You always have a method that is at least three orders of magnitude faster than what I think of, and usually completely inscrutable to me lol. Nice Jul 28, 2016 at 20:44
• @JasonB: Habit, from days of limited memory/limited CPU assembly programming. Downside for me: I'll revisit old quick-n-dirty, not commented code of mine and do a "WTF is this doing and how???"...
– ciao
Jul 28, 2016 at 20:49
• Count on ciao to come up with something completely illegible, but be sure, it'll never unpack arrays or copy them with O(n) complexity. +1 Jul 29, 2016 at 7:33

kglr's answer is really clever, but I'll just post this as well.

l /. x_ /; MemberQ[x, 0] :> ReplacePart[x, -1 -> 0]


or a slightly shorter variant using patterns instead of of MemberQ, from JHM.

l /. a : {___, 0, ___} :> ReplacePart[a, -1 -> 0]


Shorter still, you use patterns instead of ReplacePart, from Coolwater

l /. {a___, 0, b___, _} :> {a, 0, b, 0}

• a variant I like: l /. a:{b___, _} /; MemberQ[a, 0] :> {b, 0}. Jul 29, 2016 at 1:28
• @JasonB I like your ReplaceAll answers because one can learn more about the essence of programming in Mathematica, i.e. rules and patterns and in my opinion they add clarity and code maintainability. In the spirit of rules transformation, kglr and rcollyer answers work fine for me. Jul 29, 2016 at 8:20

A bit differently than in the answers so far. Mapping on sublists:

If[MemberQ[#, 0] && Last[#] =!= 0, Most[#]~Append~0, #] & /@ l


Pattern matching:

l /. {pre__, _} /; MemberQ[{pre}, 0] :> {pre, 0}

• I'm not sure what saves more time - cutting on the unnecessary Append if Last[#] === 0 or dropping the Last[#] =!= 0 test. Jul 29, 2016 at 8:17
• @LLlAMnYP It doesn't save space but I think the additional test saves time. It's rarely used and when it's, it surely has to beat construction.
– BoLe
Jul 29, 2016 at 9:21

Just an alternative

MapAt[0 &, l, {#, -1} & @@@ Position[l, 0, {2}]]


or

ReplacePart[l, {#, -1} -> 0 & @@@ Position[l, 0, {2}]]