Thread overview
Concatenation of array and range
Jan 19
Sergey
Jan 19
user1234
Jan 26
torhu
January 19

Hello there,

is it possible to concat an array and a range? In Python one would have
"""
list1 = [1, 2, 3]
list1 += array.array('L', range(a, b))
"""

The equivalent in D should be something like
"""
ulong[] list1 = [1, 2, 3];
list1 ~= iota(a, b);
"""

I haven't found a way to do this. The only way to do it seems to extend "list1" and then iterate over the range and insert the elements.

January 19

On Sunday, 19 January 2025 at 18:57:51 UTC, Samuel Redding wrote:

>

I haven't found a way to do this. The only way to do it seems to extend "list1" and then iterate over the range and insert the elements.

import std;

void main() {
    ulong[] list1 = [1, 2, 3];
    ulong a = 7;
    ulong b = 10;
	list1 ~= iota(a,b).array;
    writeln(list1); // [1, 2, 3, 7, 8, 9]
}
January 19

On Sunday, 19 January 2025 at 18:57:51 UTC, Samuel Redding wrote:

>

Hello there,

is it possible to concat an array and a range? In Python one would have
"""
list1 = [1, 2, 3]
list1 += array.array('L', range(a, b))
"""

The equivalent in D should be something like
"""
ulong[] list1 = [1, 2, 3];
list1 ~= iota(a, b);
"""

I haven't found a way to do this. The only way to do it seems to extend "list1" and then iterate over the range and insert the elements.

The exact D equivalent of your Python code would be to use std.array.array to create an array with the range's elements. For example:

import std.range : iota;
import std.array : array;

ulong[] list1 = [1, 2, 3];
list1 ~= iota(4UL, 10UL).array;

There is also another way to do this which avoids allocating (and immediately discarding) a temporary array:

import std.range : iota;
import std.array : appender;
import std.algorithm : copy;

ulong[] list1 = [1, 2, 3];
copy(iota(4UL, 10UL), appender(&list1));

In this code:

  • appender(&list1) creates an output range which appends each element inserted into it to list1
  • copy takes each element from an input range (in this case, iota(4UL, 10UL)) and inserts it into an output range (in this case, appender(&list1)).
January 19

On Sunday, 19 January 2025 at 18:57:51 UTC, Samuel Redding wrote:

>

Hello there,

is it possible to concat an array and a range? In Python one would have
"""
list1 = [1, 2, 3]
list1 += array.array('L', range(a, b))
"""

The equivalent in D should be something like
"""
ulong[] list1 = [1, 2, 3];
list1 ~= iota(a, b);
"""

I haven't found a way to do this. The only way to do it seems to extend "list1" and then iterate over the range and insert the elements.

iota is a lazy range. As shown in the other answer you can yield the lazy thing using .array. But assuming it's ok to create a distinctive iterator you can also chain the two things:

auto list2 = std.range.chain(list1,iota(a,b));

with list2 being fully lazy.

January 19

On Sunday, 19 January 2025 at 19:35:31 UTC, user1234 wrote:

>

On Sunday, 19 January 2025 at 18:57:51 UTC, Samuel Redding wrote:

>

Hello there,

is it possible to concat an array and a range? In Python one would have
"""
list1 = [1, 2, 3]
list1 += array.array('L', range(a, b))
"""

The equivalent in D should be something like
"""
ulong[] list1 = [1, 2, 3];
list1 ~= iota(a, b);
"""

I haven't found a way to do this. The only way to do it seems [...]

Thanks a lot to all of you!

January 26

On Sunday, 19 January 2025 at 18:57:51 UTC, Samuel Redding wrote:

>

Hello there,

is it possible to concat an array and a range? In Python one would have
"""
list1 = [1, 2, 3]
list1 += array.array('L', range(a, b))
"""

For the record, you can extend a list using a range object in Python:

list1.extend(range(a, b))

or if you just want to iterate over them in order:

iter = itertools.chain(list1, range(a, b))