Message Boards Message Boards


Can you give a name to list outputs? (see code)

Posted 5 years ago
13 Replies
5 Total Likes

The title of this questions doesn't really make sense. But what I am trying to do is give names to each output so that I wont have to refer to my input to know which output corresponds with each list. This will make more sense in this basic example:

Here's my input:

list := {{1, 5, 4}, {5, 9, 7}, {8, 6, 9}, {3, 5, 4}};
Plus @@@ list // TableForm

Here's my output:


I could easily look at my list of 4 and know which output refers to each list. But if I had 100 or 1000 different lists then this could be exhausting to find which list refers to which output..Is there a way to give a name to each output? like "List 1" or something next to 10, "List 2" next to 21 and so on... Sorry if this is hard to follow but I tried to be as specific as possible.

Thanks for any help or advice, Brandon.

13 Replies
Posted 5 years ago

You can assign between structures as long as they conform:

In[1]:= list := {{1, 5, 4}, {5, 9, 7}, {8, 6, 9}, {3, 5, 4}};

In[2]:= {s1, s2, s3, s4} = Plus @@@ list;

In[3]:= s2

Out[3]= 21
Posted 5 years ago

Hey David,

That's pretty cool. I implemented that method to what I'm doing and I like it. I still want to see if theres a way to correspond each list to each output, but having it all show at once.

Something along the lines of

10 List 1
21 List 2
23 List 3

and so on... This might not even be possible but I'm sure going to try to figure it out.

I appreciate your method, that's what I'll be using for the meantime



list = {{1, 5, 4}, {5, 9, 7}, {8, 6, 9}, {3, 5, 4}};
TableForm[Plus @@@ list, TableHeadings -> Automatic]

I also changed your := to = which makes more sense in this case...

Posted 5 years ago

Is there a way to add a "title" or header to this codes output?? Like so it says "Lists" in the first column and maybe "Values" in the second column.. I was thinking AppendTo might work..

Any suggestions?

Thanks for any help..


Posted 5 years ago

Hi Brandon, Please see the attached notebook. It has formatted output, which is messy in a forum post, but clean in a notebook.




Associations are nice for something like this.

list := {{1, 5, 4}, {5, 9, 7}, {8, 6, 9}, {3, 5, 4}};
caseData =  Association @@ Table[data[i] -> list[[i]], {i, 1, Length[list]}]


<|data[1] -> {1, 5, 4}, data[2] -> {5, 9, 7}, data[3] -> {8, 6, 9},  data[4] -> {3, 5, 4}|> 

You can then Map any function on the caseData Association.

Total /@ caseData


<|data[1] -> 10, data[2] -> 21, data[3] -> 23, data[4] -> 12|>

Or you could list the results in a column by:

Total /@ caseData // Normal // Column

data[1]-> 10
data[2]-> 21
data[3]-> 23
data[4]-> 12
Posted 5 years ago

Thank you all for your help. I'm going to explore all the methods. I tend to use a delayed assignment in more cases then I don't. I always feel like it is more useful, I thought immediate assignments only work locally and cannot be called upon later? Either way, thanks again!

Best to all,


Immediate and Delayed assignments have the same context and domain of applicability. Sometimes there may be a reason to use delayed and sometimes a reason to use immediate. Often you can use either. In the example of caseData in my reply using a delayed assignment would cause the Association to be recalculated each time you used it, which for a very long list might be inefficient.

Posted 5 years ago

For large lists of say 1000+ How would say "1345 list 345" help, I mean how would you know which was list 345 in the original list? Perhaps something along these lines would be of use, where a copy of the original list is included with the total, etc.

list = Partition[Range[30], 3];
list = Partition[Riffle[list, Total /@ list], 2]; Column[list]

Change the range value and partition size to suit.

Posted 5 years ago

Hey Paul,

I know exactly what you mean.. II was able to use the commands "Map" and "Apply" and essentially my output is 1 value along with the list right next to it in a nice grid format. I guess my next challenge is somehow adding a header to both of those columns so that if someone only saw the output, they would know what each column represents. I'm going to use your method also, it seems very valuable especially like you said, with a 1000+ list or even more.

I appreciate the input and help!


Posted 5 years ago

Along these lines.

list = Partition[Range[50], 5];
list = Partition[Riffle[list, Total /@ list], 
  2]; PrependTo[list, {"List", "Sum of list"}]; Grid[list, 
 Background -> {None, {Green}}, Alignment -> Left, Frame -> All, 
 Spacings -> {1, 1}]


Posted 5 years ago

Hey Paul,

I appreciate your help, a lot.

I'm going to attach a specific notebook I'm working on. I found a "way around" adding headings. If you evaluate the notebook as is, you will get a nice 2 column table(which is the output I need for my task). If I click on the actual table output, there is an "add headings" option, which is great and all but that's kind of a "way around" it like I said. I am having trouble using Partition/Riffle in the code I currently have. I don't necessarily want you to do it for me, but I have tried over and over and things just get messy and it doesn't work. As of now, I am sticking with the "add headings" I was able to use by clicking on the table. I'm sure I will figure it out at some point, but I sure have been wasting time. You don't have to understand the context of the problem I'm working on, I have completed the task of the assignment, though I want to add some header but it seems to require much more work then I expected. I actually think I can remove the //tableform out of my function and wrap the entire function in tableform and add headings that way. That seemed to make things messy too.


I appreciate you sticking through this with me.

Best, Brandon.


I would like to do a bit more advocacy for the use of Associations. This discussion has been useful to me because it caused me to look more deeply into Associations. By attaching data to keys, Associations make it much easier to manipulate and extract subsets of the data while maintain identifications. Even a short list of numbers can be better as an Association because it provides context to the numbers,

Here we construct a longer list of integer triplets as test data. (The results will vary depending on the random set generated.)

list = Array[RandomInteger[{1, 99}] &, {1000, 3}];
caseData =  Association @@ Table[data[i] -> list[[i]], {i, 1, Length[list]}];

We would probably not wish to look at a list that's a 1000 items long. We can easily look at specific items.

KeyTake[caseData, {data[122], data[453]}]


<|data[122] -> {95, 69, 21}, data[453] -> {42, 79, 13}|>

We can make a new association that contains the totals of each triplet list, and find the min and max values.

totalData = Total /@ caseData;


{22, 265}

We can select data by a specific criterion.

range1Data = Select[totalData, 100 <= # <= 105 &] // Sort;
Labeled[Dataset[range1Data], "Totals between 100 and 105", Top]

(I don't know how to paste an image of a Dataset into a posting. You will have to run the code. It gives a table list of the data with controls for moving through the table.)

We can extract the matching underlying data.

range1Keys = Keys[range1Data];
   range1Keys], "Underlying data for\ntotals between 100 and 105",  Top]

Or we can assemble a data association that composes the totals and underlying data and display it with headings.

range1Composite = 
   Table[case -> 
     Association["triplets" -> caseData[case], 
      "total" -> range1Data[case]], {case, range1Keys}];
Labeled[Dataset[range1Composite], "Totals between 100 and 105", Top]

This gives a table with both row and column headings and an overall label at the top, and controls for scrolling through it.

Associations and Datasets are a versatile method for examining and performing calculations on sets of data.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract