i.e append an array of elements into another array:
x = [1, 2, 3]
x.extend([4, 5])
print(x) # [1, 2, 3, 4, 5]
Thanks.
Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
June 21, 2021 what is D's idiom of Python's list.extend(another_list)? | ||||
---|---|---|---|---|
| ||||
i.e append an array of elements into another array:
Thanks. |
June 21, 2021 Re: what is D's idiom of Python's list.extend(another_list)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On Monday, 21 June 2021 at 05:36:36 UTC, mw wrote: >i.e append an array of elements into another array:
Thanks.
|
June 21, 2021 Re: what is D's idiom of Python's list.extend(another_list)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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:
Thanks.
Ha! great. I didn't know |
June 21, 2021 Re: what is D's idiom of Python's list.extend(another_list)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On Monday, 21 June 2021 at 06:16:15 UTC, mw wrote: >Ha! great. I didn't know
|
June 21, 2021 Re: what is D's idiom of Python's list.extend(another_list)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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] */
}
|
June 21, 2021 Re: what is D's idiom of Python's list.extend(another_list)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On 6/20/21 10:36 PM, mw wrote:
> i.e append an array of elements into another array:
>
>
> ```Python
> x = [1, 2, 3]
> x.extend([4, 5])
> print(x) # [1, 2, 3, 4, 5]
> ```
>
> Thanks.
There is also std.range.chain, which can visit multiple ranges in sequence without copying elements. This is a lifesaver when the arrays are very large.
import std.range;
import std.algorithm;
void main() {
auto a = [ 1, 2, 3 ];
auto b = [ 4, 5 ];
auto expected = iota(1, 6);
assert(chain(a, b).equal(expected));
}
Ranges can be very useful e.g. to sort elements of different random access ranges:
import std.range;
import std.algorithm;
void main() {
auto a = [ 5, 1, 3 ];
auto b = [ 4, 2 ];
auto expected = iota(1, 6);
// This time we sort:
assert(chain(a, b).sort.equal(expected));
// What? :)
assert(a == [ 1, 2, 3]);
assert(b == [ 4, 5 ]);
}
Ali
|
June 21, 2021 Re: what is D's idiom of Python's list.extend(another_list)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | 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.
|