# How can I iteratively add elements to a list

Basically I want to make a histogram with a list of dates by using DateHistogram[]. However I have the data of the dates in nested list format, with elements like {2020,6,26}, and although I know how to convert it to a date by using DateObject[{2020,6,26}] I can not construct a list of dates by iteration. I am trying the following:

For[i = 0, i < 5, i++, Insert[egunak, DateObject[watchdates[[i]]], 1]]


where watchdates is the nested list of {2020,6,26} date elements and egunak is the list I want to create with the dates (I have tried it to define it before).

I have also tried using Append instead of Insert or by iterating with Do, but didn't succeed. I am pretty new with the Wolfram language, sorry.

• Is egunak = watchdates[[;;4]] what you are looking for? – Edmund Jun 26 at 22:31
• Be sure to check out AppendTo and compare it with Append. Also, Do is better than For because it's less error prone, but functional solutions like Map is the best of all in almost all situations. (Easier to read and has better performance.) – C. E. Jun 27 at 8:43
• I see, alright thanks a lot!! – Andoni Royo Jun 28 at 15:54

You can Map the DateObject[] function onto each member of your watchdates list like in

Map[DateObject, {{2020, 6, 26}, {2020, 6, 25}}]


which yields a list of DateObjects.

There is also a shorter symbolic form for Map

DateObject /@ {{2020, 6, 26}, {2020, 6, 25}}


Is this what you had in mind?

• Yeeees!! that exactly what I wanted. I didn't know about the Map function – Andoni Royo Jun 26 at 23:24