title: Erlang:creating list of tuples within lists:foreach
body: <p>I query the list of users from Mnesia Database in Chicagoboss. I'm getting the error when I try to add the Lists within lists:foreach with ++ operator. My aim is, based on userid I will do ets:lookup to my cache and create a List like - [{{&lt;&lt;"name">>,&lt;&lt;"Batman">>}, {&lt;&lt;"steps">>,2552}, {&lt;&lt;"distance">>,2050}}].</p>
  
  <p>For each user I'll create this list and add with the previous List. So that ultimately I can can sort on &lt;&lt;"steps">> and convert the binary list by json encoding and send it to the client via Websockets.</p>
  
  <p>I'm getting the error at this line:
  Reading1 = Reading2 ++ Currentlist</p>
  
  <p>as I've decalred Reading1 as an Empty list. </p>
  
  <p>My question is how can I manipulate the lists within the lists:foreach and then send the result List via websocket? </p>
  
  <pre><code>        BelugaUsers = boss_db:find(users, [{accesstoken, 'not_equals', ''}]),
          Reading1 = [],
          Reading2 = [],
          lists:foreach(fun(X) -&gt;         
          {_,_,BEmail,BName,_,_,BAccessToken,_} = X,
          UserKey = BEmail ++ "-" ++ ?MYAPICALL1,
          io:format("UserKey for Leader Board: ~n~p~n",[UserKey]),
          [Reading] = ets:lookup(myapi_cache, list_to_binary(UserKey)),
          {_,Result} = Reading,
  
          ActivitySummary = proplists:get_value(&lt;&lt;"activitySummary"&gt;&gt;, Result),
          %Print ActivitySummary for the user ....printing fine 
          io:format("ActivitySummary ==========: ~n~p~n",[ActivitySummary]),
  
          %Create a list of the format
          %[{{&lt;&lt;"name"&gt;&gt;,&lt;&lt;"Batman"&gt;&gt;}, {&lt;&lt;"steps"&gt;&gt;,2552}, {&lt;&lt;"distance"&gt;&gt;,2050}}]
  
          Currentlist = [{{&lt;&lt;"name"&gt;&gt;, list_to_binary(BName)}, {&lt;&lt;"steps"&gt;&gt;, proplists:get_value(&lt;&lt;"steps"&gt;&gt;, ActivitySummary)}, {&lt;&lt;"distance"&gt;&gt;, proplists:get_value(&lt;&lt;"distance"&gt;&gt;, ActivitySummary)}}],
          %% HERE I'M GETTING error%%
          Reading1 = Reading2 ++ Currentlist
          end, BelugaUsers),
          %sort the list
          Reading3 = lists:keysort(2, Reading1),
          %reverse the list
          Reading4 = lists:reverse(Reading3),
          WebSocketId ! {text, jsx:encode(Reading4)},
  </code></pre>