10
$\begingroup$

Suppose I have a nested list of the following:

list={{1,2},{5,8},{4,1}...} 

I would like to add a number to each first term of the list. The output would be like the following:

{{1+x,2},{5+x,8},{4+x},1}...}

$\endgroup$
2
  • 5
    $\begingroup$ {x, 0}+#&/@list $\endgroup$ Commented Feb 5, 2016 at 14:43
  • $\begingroup$ Worked great! Thanks! $\endgroup$ Commented Feb 5, 2016 at 14:47

9 Answers 9

19
$\begingroup$
list = {{1, 2}, {5, 8}, {4, 1}}; 

Apply, Function:

{#1 + x, #2} & @@@ list (* {{1 + x, 2}, {5 + x, 8}, {4 + x, 1}} *) 

Map:

# + {x, 0} & /@ list (* {{1 + x, 2}, {5 + x, 8}, {4 + x, 1}} *) 

Part, Transpose:

Transpose[{list[[All, 1]] + x, list[[All, 2]]}] (* {{1 + x, 2}, {5 + x, 8}, {4 + x, 1}} *) 

MapAt:

MapAt[x + # &, list, {All, 1}] (* {{1 + x, 2}, {5 + x, 8}, {4 + x, 1}} *) 

Transpose only:

Transpose[Transpose@list + {x, 0}] (* {{1 + x, 2}, {5 + x, 8}, {4 + x, 1}} *) 

Replace:

Replace[list, {a_, b_} :> {a + x, b}, {1}] (* {{1 + x, 2}, {5 + x, 8}, {4 + x, 1}} *) 

Inner:

Inner[Plus, list, {x, 0}, List] (* {{1 + x, 2}, {5 + x, 8}, {4 + x, 1}} *) 
$\endgroup$
6
  • $\begingroup$ Don't forget MapAt[x + # &, list, {All, 1}] $\endgroup$ Commented Feb 5, 2016 at 14:44
  • $\begingroup$ I like this format really much $\endgroup$ Commented Feb 5, 2016 at 14:45
  • 2
    $\begingroup$ @JasonB Great, made it community wiki feel free to edit! $\endgroup$ Commented Feb 5, 2016 at 14:45
  • 1
    $\begingroup$ @march The problem with ReplaceAll is: what if Length[list] == 2? $\endgroup$ Commented Feb 5, 2016 at 17:07
  • $\begingroup$ @Szabolcs. Oh. Agreed. I like the Replace version better, since fixing the ReplaceAll version by doing, for instance, a_Symbol won't be general enough.Those not-so-corner cases kill me sometimes. $\endgroup$ Commented Feb 5, 2016 at 17:09
5
$\begingroup$
list = {{1, 2}, {5, 8}, {4, 1}}; 

Using Threaded (new in 13.1)

list + Threaded[{x, 0}] 

{{1 + x, 2}, {5 + x, 8}, {4 + x, 1}}

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

Using Cases:

Cases[list, {a_, b_} :> {a + x, b}] (*{{1 + x, 2}, {5 + x, 8}, {4 + x, 1}}*) 
$\endgroup$
4
$\begingroup$

Using SubsetMap:

list = {{1, 2}, {5, 8}, {4, 1}}; SubsetMap[# + x &, list, {All, 1}] 

{{1 + x, 2}, {5 + x, 8}, {4 + x, 1}}

$\endgroup$
4
$\begingroup$

First and foremost, I have to say that the solution using Threaded looks like the nicest one to me. Kudos to @eldo.

I want to demonstrate the use of ThroughOperator that was developed by @Sjoerd Smit. To the extend of my knowledge it was first suggested in this answer.

In this example we do

ResourceFunction["ThroughOperator"][{#1 + x &, #2 &}] @@@ list 

{{1 + x, 2}, {5 + x, 8}, {4 + x, 1}}

A not so normal way of doing it is to use Outer

Transpose[{First /@ Join @@@ Outer[Plus, list, {x, 0}], Last /@ Join @@@ Outer[Plus, list, {x, 0}]}] 
$\endgroup$
4
$\begingroup$
list + ConstantArray[{x,0},Length@list] (* {{1+x,2},{5+x,8},{4+x,1}} *) 
$\endgroup$
3
$\begingroup$

For a list with dimension {n, 2}:

list = {{1, 2}, {5, 8}, {4, 1}}; list[[All, 1]] += x ; list (* {{1+x,2},{5+x,8},{4+x,1}} *) list = {{1, 2}, {5, 8}, {4, 1}}; list[[All, 1]] += {x, y, z} ; list (* {{1+x,2},{5+y,8},{4+z,1}} *) 
$\endgroup$
2
$\begingroup$
list = {{1, 2}, {5, 8}, {4, 1}}; 

Using ReplaceAt (new in 13.1)

ReplaceAt[a_ :> a + x, {All, 1}] @ list 

{{1 + x, 2}, {5 + x, 8}, {4 + x, 1}}

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

Another way using ReplaceList:

ReplaceList[list, {___, {a_, b_}, ___} :> {a + x, b}] 

{{1 + x, 2}, {5 + x, 8}, {4 + x, 1}}

$\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.