Initializing an empty array for a range of combinations in Julia

Viewed 85

I am trying to initialize an empty array for a range(3) such that each of the array is initialized 3 times. For example, in [1 2 3] each array has index of 12, 13, 23. A python syntax for such task is,

a, b, c = [[] for count in range(3)]

However, I would like to know the correct syntax to execute this in Julia?

1 Answers

The Julian equivalent of your Python code would be:

a, b, c = (Int[] for count in 0:2)

Notes:

  • using () instead of [] makes less memory allocations. You could still use [] though
  • it is better to have typed Vectors so here I am creating empty Vectors of Int
Related