Message Boards Message Boards

0
|
15458 Views
|
7 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Appending more than one element to a list

Posted 10 years ago

Why is it that the built-in Append function only appears to replace the last element in the list after an element has been added to an empty list? See example below:

anotherList = {""};
If[3 > 2, Append[anotherList, {"a"}], Print["number not greater"]]
If[3 > 2, Append[anotherList, {"b"}], Print["number not greater"]]
Append[anotherList, "c"]
Append[anotherList, "d"]
Length[anotherList]

{"", {"a"}}
{"", {"b"}}
{"", "c"}
{"", "d"}
POSTED BY: Bob Stephens
7 Replies
Posted 10 years ago

ok, this is crazy...........it appears that within the scope of a notebook you can only use append to set the last element of the notebook? I check and if this is a bug it was present in Mathematica 9 as well. I would have expected multiple calls to the Append function to be able to append additional items to the same original List - am I missing something?

myList = {"a"}
myList2 = Append[myList, "b"]
myList3 = Append[myList2, "c"]


{"a"}
{"a", "b"}
{"a", "b", "c"}
POSTED BY: Bob Stephens
Posted 10 years ago

Well it looks like AppendTo is the solution to this problem. It would be nice if the Mathematica documentation allowed the user to discover this more easily. Looks like Append is using an absolute memory reference to the original instance of "myList" each time Append is called. See sample code below:

myList = {"a"}
myList2 = Append[myList, "b"]
myList3 = Append[myList2, "c"]


{"a"}
{"a", "b"}
{"a", "b", "c"}
POSTED BY: Bob Stephens
Posted 10 years ago

Append appends an element to the list and returns the result. It does not modify the original list. It used to be that if you wanted to do that you would use list = Append[list element]. (And you still can.)

AppendTo came later. It actually modifies the list itself, so the Set is not required.

POSTED BY: David Keith

Actually it's a fundamental principle in Mathematica that functions shouldn't change their arguments. Instead, they return modified data:.

For example, this is a list:

In[1]:= a = {1, 2, 3}
Out[1]= {1, 2, 3}

This returns a new list which has 4 appended at the end ...

In[2]:= Append[a, 4]
Out[2]= {1, 2, 3, 4}

... but it does not change a:

In[3]:= a
Out[3]= {1, 2, 3}

The functions that do actually change variables are a tiny minority, and include = and := (obviously); +=, *= and similar ones such as ++, --; and finally AppendTo and PrependTo.

Regarding discoverability, once you get used to how Mathematica works, this behaviour will feel natural and you'll realize that it makes functions behave predictably. Some other languages have both variety, functions like Append and functions like AppendTo, for many tasks, and you'll always have to check the documentation to see what a given function does precisely.

Mathematica does not have this problem. Does, e.g., Sort[a] change the order of the elements in a? No, it doesn't, it just returns a new list with elements rearranged.

Some more reading: * http://mathematica.stackexchange.com/a/42270/12

If you are considering appending multiple times in a loop, check Sow and Reap instead.

POSTED BY: Szabolcs Horvát
Posted 10 years ago

I quite agree with Szabolcs. I was surprised when AppendTo appeared in the language. And I am not at all sure the convenience justifies the inconsistency it introduces.

POSTED BY: David Keith

You must instruct program to alter variable a,Like this , a=Append[a,4].

Posted 3 years ago

If you want to append a list to another list you could also use the "Join" function: Join[{a,b},{c,d}] = {a,b,c,d}

POSTED BY: pim Herbschleb
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract