Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6877721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:38:06+00:00 2026-05-27T04:38:06+00:00

If I do the following in Mathematica f[l_] := Module[{}, l[[1]] = Append[l[[1]], 3];

  • 0

If I do the following in Mathematica

f[l_] := Module[{}, l[[1]] = Append[l[[1]], 3]; l]
f[{{}, 3}]

I get an error:

Set::setps: "{{},3} in the part assignment is not a symbol. "

Even l={{}, 3};f[l] gets the same error. But I can do f[l_] := Module[{}, {Append[l[[1]], 3],l[[2]]}] or l = {{}, 3}; l[[1]] = Append[l[[1]], 3]; l.

What is your explanation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:38:07+00:00Added an answer on May 27, 2026 at 4:38 am

    There are multiple problems here:

    1. Attempting Part assignment on a non-Symbol, just as the error message states.

    2. Attempting to manipulate a named replacement object as though it were a symbol.

    The replacement that takes place in this construct:

    f[x_] := head[x, 2, 3]
    

    Is analogous to that of With:

    With[{x = something}, head[x, 2, 3]]
    

    That is, the substitution is made directly and before evaluation, such that the function Head never even sees an object x. Look what happens with this:

    ClearAll[f,x]
    x = 5;
    f[x_] := (x = x+2; x)
    
    f[x]
    
    During evaluation of In[8]:= Set::setraw: Cannot assign to raw object 5. >>
    
    Out[]= 5

    This evaluates as: (5 = 5+2; 5) so not only is assignment to 5 impossible, but all instances of x that appear in the right hand side of := are replaced with the value of x when it is fed to f. Consider what happens if we try to bypass the assignment problem by using a function with side effects:

    ClearAll[f, x, incrementX]
    
    incrementX[] := (x += 2)
    x = 3;
    incrementX[];
    x
    
    5

    So our incrementX function is working. But now we try:

    f[x_] := (incrementX[]; x)
    
    f[x] 
    
    5

    incrementX did not fail:

    x
    
    7

    Rather, the the value of x was 5 at the time of evaluation of f[x] and therefore that is returned.


    What does work?

    What options do we have for things related to what you are attempting? There are several.

    1. Use a Hold attribute

    We can set a Hold attribute such as HoldFirst or HoldAll on the function, so that we may pass the symbol name to RHS functions, rather than only its value.

    ClearAll[heldF]
    SetAttributes[heldF, HoldAll]
    
    x = {1, 2, 3};
    
    heldF[x_] := (x[[1]] = 7; x)
    
    heldF[x]
    x
    <pre>{7, 2, 3}</pre>
    <pre>{7, 2, 3}</pre>
    

    We see that both the global value of x, and the x expression returned by heldF are changed. Note that heldF must be given a Symbol as an argument otherwise you are again attempting {1, 2, 3}[[1]] = 7.

    2. Use a temporary Symbol

    As Arnoud Buzing shows, we can also use a temporary Symbol in Module.

    ClearAll[proxyF]
    
    x = {1, 2, 3};
    
    proxyF[x_] := Module[{proxy = x}, proxy[[1]] = 7; proxy]
    
    proxyF[x]
    proxyF[{1, 2, 3}]
    x
    
    {7, 2, 3}
    {7, 2, 3}
    {1, 2, 3}

    3. Use ReplacePart

    We can also avoid symbols completely and just use ReplacePart:

    ClearAll[directF]
    
    x = {1, 2, 3};
    
    directF[x_] := ReplacePart[x, 1 -> 7]
    
    directF[x]
    x
    
    {7, 2, 3}
    {1, 2, 3}

    This can be used for modifications rather than outright replacements as well:

    ClearAll[f]
    
    f[l_] := ReplacePart[l, 1 :> l[[1]] ~Append~ 3]
    
    f[{{}, 3}]
    
    {{3}, 3}
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I never had an overflow error in Mathematica, the following happened. I demo-ed the
I would like to set up the following custom notation in Mathematica 7. This
When I try to calculate the following integral in Mathematica 8, I get this
Consider the following dialog with the command-line interface to the kernel: $ math Mathematica
Why does the following code Graphics[Raster[{{Hue[1], Hue[1/3]}, {Hue[2/3], Hue[1/6]}}]] not produce any output? In
I am trying to plot a few points on the following picture in Mathematica:
All of the following refers to music/MIDI ( SoundNote objects), not sampled sounds. Unfortunately
Can someone offer some advice on how to get started with mathematica packages? I
In the following bit of mathematica code a1 = {{0, -I}, {I, 0}} a2
The following Mathematica function f creates a string of whitespace of length n. f[n_]:=Fold[StringJoin,,Array[

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.