www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - what is D's idiom of Python's list.extend(another_list)?

reply mw <mingwu gmail.com> writes:
i.e append an array of elements into another array:


```Python
x = [1, 2, 3]
x.extend([4, 5])

```

Thanks.
Jun 20 2021
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Monday, 21 June 2021 at 05:36:36 UTC, mw wrote:
 i.e append an array of elements into another array:


 ```Python
 x = [1, 2, 3]
 x.extend([4, 5])

 ```

 Thanks.
```d x ~= [4, 5]; ```
Jun 20 2021
parent reply mw <mingwu gmail.com> writes:
On Monday, 21 June 2021 at 06:04:56 UTC, Mike Parker wrote:
 On Monday, 21 June 2021 at 05:36:36 UTC, mw wrote:
 i.e append an array of elements into another array:


 ```Python
 x = [1, 2, 3]
 x.extend([4, 5])

 ```

 Thanks.
```d x ~= [4, 5]; ```
Ha! great. I didn't know `~` works for both single elements and array!
Jun 20 2021
parent reply Mike Parker <aldacron gmail.com> writes:
On Monday, 21 June 2021 at 06:16:15 UTC, mw wrote:

 Ha! great. I didn't know `~` works for both single elements and 
 array!
`~` by itself is the concatenation operator and only works with two array operands. `~=` is the append operator and can append arrays or single elements.
Jun 21 2021
parent reply ag0aep6g <anonymous example.com> writes:
On 21.06.21 09:02, Mike Parker wrote:
 On Monday, 21 June 2021 at 06:16:15 UTC, mw wrote:
 
 Ha! great. I didn't know `~` works for both single elements and array!
`~` by itself is the concatenation operator and only works with two array operands. `~=` is the append operator and can append arrays or single elements.
`~` works just fine with single elements: void main() { import std.stdio; int[] a = [2, 3, 4]; writeln(1 ~ a ~ 5); /* [1, 2, 3, 4, 5] */ }
Jun 21 2021
parent Mike Parker <aldacron gmail.com> writes:
On Monday, 21 June 2021 at 08:40:47 UTC, ag0aep6g wrote:
 `~` works just fine with single elements:

     void main()
     {
         import std.stdio;
         int[] a = [2, 3, 4];
         writeln(1 ~ a ~ 5); /* [1, 2, 3, 4, 5] */
     }
Cool. I've had it in my head for many years now that this was not a thing.
Jun 21 2021
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 6/20/21 10:36 PM, mw wrote:
 i.e append an array of elements into another array:
=20
=20
 ```Python
 x =3D [1, 2, 3]
 x.extend([4, 5])

 ```
=20
 Thanks.
There is also std.range.chain, which can visit multiple ranges in=20 sequence without copying elements. This is a lifesaver when the arrays=20 are very large. import std.range; import std.algorithm; void main() { auto a =3D [ 1, 2, 3 ]; auto b =3D [ 4, 5 ]; auto expected =3D iota(1, 6); assert(chain(a, b).equal(expected)); } Ranges can be very useful e.g. to sort elements of different random=20 access ranges: import std.range; import std.algorithm; void main() { auto a =3D [ 5, 1, 3 ]; auto b =3D [ 4, 2 ]; auto expected =3D iota(1, 6); // This time we sort: assert(chain(a, b).sort.equal(expected)); // What? :) assert(a =3D=3D [ 1, 2, 3]); assert(b =3D=3D [ 4, 5 ]); } Ali
Jun 21 2021