4
$\begingroup$

Sorry, for the broad title, didn't know how to best describe it. I have a list likes this:

list = {{{1, 2, 3, {4, 5, 6}}, {1, 2, 3, {4, 5, 6}}}, {{1, 2, 3, {4, 5, 6}}, {1, 2, 3, {4, 5, 6}}}} 

And I want to replace the second part of the list elements, i.e. {4,5,6} everywhere by just the first element of this part, i.e. 4. the list should then look like this:

list2 = {{{1, 2, 3, 4}, {1, 2, 3, 4}}, {{1, 2, 3, 4}, {1, 2, 3, 4}}} 

I can do it with some complicated constructions looping through all the list elements by means of Table but there must be an easier way by using Replace or MapAtand I just can't figure it out.

Any help appreciated!

$\endgroup$

5 Answers 5

6
$\begingroup$
list2 = Replace[list, {a_, ___} -> a, {3}] list2 = MapAt[First, list, {All, All, -1}] 

{{{1, 2, 3, 4}, {1, 2, 3, 4}}, {{1, 2, 3, 4}, {1, 2, 3, 4}}}

If list is very big I think the fastest one would be

Join[Drop[list, None, None, -1], list[[All, All, {-1}, 1]], 3] 
$\endgroup$
3
  • $\begingroup$ Thanks Coolwater! Can you maybe comment on the first line that uses Replace a bit, I don't understand what exactly you did there ;). Especially this part {a_, ___}->a $\endgroup$ Commented Aug 17, 2017 at 13:45
  • 1
    $\begingroup$ In the deepest level {3} we need to replace elements with the form {__} (i.e. a list with one or more elements). I write instead {a_, ___} (one element plus zero or more elements) to keep a reference only to what's needed $\endgroup$ Commented Aug 17, 2017 at 14:01
  • $\begingroup$ Ah I see, thank you! $\endgroup$ Commented Aug 17, 2017 at 14:05
6
$\begingroup$

Also:

ClearAll[f1, f2, f3, ☺] f1 = ReplacePart[#, {_, _, -1, 0} :> (# &)] &; f2 = Module[{l = #}, l[[All, All, -1, 0]] = # &; l] &; f3 = Module[{l = #}, l[[All, All, -1]] = l[[All, All, -1, 1]]; l] &; ☺ = # & @@@ {##} & @@@ # & /@ # &; (* for fun *) f1 @ list 

{{{1, 2, 3, 4}, {1, 2, 3, 4}}, {{1, 2, 3, 4}, {1, 2, 3, 4}}}

Equal @@ (#@list & /@ {f1, f2, f3, ☺}) 

True

$\endgroup$
1
  • $\begingroup$ So many solutions, thanks :)! $\endgroup$ Commented Aug 17, 2017 at 14:44
4
$\begingroup$

Using Map and AtomQ:

Map[If[AtomQ[#], #, ##[[1]]] &, list, {3}] (*{{{1, 2, 3, 4}, {1, 2, 3, 4}}, {{1, 2, 3, 4}, {1, 2, 3, 4}}}*) 
$\endgroup$
4
$\begingroup$
Clear["Global`*"]; 

With another similar entry added at level 1 as an example:

list = {{{1, 2, 3, {4, 5, 6}}, {1, 2, 3, {4, 5, 6}}}, {{1, 2, 3, {4, 5, 6}}, {1, 2, 3, {4, 5, 6}}}, {1, 2, 3, {4, 5, 6}}} Replace[list, {x__Integer, k : {y__}} :> {x, First@k}, -1] 

or:

list /. {x__Integer, k : {y__}} :> {x, First@k} 

or

list /. v_?VectorQ :> First@v 

{{{1, 2, 3, 4}, {1, 2, 3, 4}}, {{1, 2, 3, 4}, {1, 2, 3, 4}}, {1, 2, 3, 4}}

$\endgroup$
3
$\begingroup$
list = {{{1, 2, 3, {4, 5, 6}}, {1, 2, 3, {4, 5, 6}}}, {{1, 2, 3, {4, 5, 6}}, {1, 2, 3, {4, 5, 6}}}}; 

Using ReplaceAt (new in 13.1)

ReplaceAt[_ :> Nothing, {All, All, -1, 2 ;;}] @ list /. {a_} :> a 

{{{1, 2, 3, 4}, {1, 2, 3, 4}}, {{1, 2, 3, 4}, {1, 2, 3, 4}}}

Using ReplaceAll

list /. {a__Integer, b_} :> {a, b[[1]]} 

{{{1, 2, 3, 4}, {1, 2, 3, 4}}, {{1, 2, 3, 4}, {1, 2, 3, 4}}}

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.