# [✓] Replace AppendTo with usage of Reap Sow?

GROUPS:
 I have tried to figure out how to use Reap and Sow from searching online, but do not understand it. Can anyone help me with my line of code? The issue is that the data set is so large that it takes a very long time to compute.I currently have a list that has just over 623,000 elements in it. My code to then loop through the list to determine the VertexDegree and then store those elements in a new list is: s35vertlist = {}; Do[AppendTo[s35vertlist, VertexDegree[Subgraph[s35, cyclelists35[[i]]]]], {i, Length[cyclelists35]}] Where s35vertlist is my new empty list, and cyclelists35 is my list with the large number of elements in it. I have tried s35vertlist = {}; s35vertlist = Reap[Do[Sow[s35vertlist, VertexDegree[Subgraph[s35, cyclelists35[[i]]]]], {i, Length[cyclelists35]}]] But it returns Null when I know that it should have least one element. So, I am hoping that someone can quickly tell me how to get Reap/Sow to work instead of AppendTo. I am sorry if this is super simple and annoying, but I can't figure it out. Thank you so much.
9 months ago
4 Replies
 Kuba Podkalicki 2 Votes You don't need Sow/Reap here: s35vertlist = Table[VertexDegree[Subgraph[s35, cyclelists35[[i]]]], {i, Length[cyclelists35]}] should do, or s35vertlist = VertexDegree[Subgraph[s35, #]] & /@ cyclelists35 With respect to a functional style, maybe there is something graph specific which could make it even better but that is not my area.
 Neil Singer 2 Votes Justin,I would think that Map might be faster: s35vertlist = Map[VertexDegree[Subgraph[s35, #]] &, cyclelists35] Sow works like this -- you don't append to the list -- it just collects the values. I do not recommend this in your case because you want a value returned for every element in your cyclelist s35vertlist = Reap[Do[Sow[VertexDegree[Subgraph[s35, cyclelists35[[i]]]]], {i, Length[cyclelists35]}]] Try doing an AbsoluteTiming[] for your loop and the Map and post the times. I am guessing that the Map is faster.Regards