What (if any) is the C# equivalent of Python's itertools.chain method?
Python Example:
l1 = [1, 2]
l2 = [3, 4]
for v in itertools.chain(l1, l2):
print(v)
Results:
1
2
3
4
Note that I'm not interested in making a new list that combines my first two and then processing that. I want the memory/time savings that itertools.chain provides by not instantiating this combined list.
You could do the same on C# by using the Concat extension method from LINQ:
l1.Concat(l2)
LINQ uses a deferred execution model, so this won't create a new list.
Enumerable.Concat
(MSDN)
var l1 = new List<int>() { 1, 2 };
var l2 = new List<int>() { 3, 4 };
foreach(var item in Enumerable.Concat(l1, l2))
{
Console.WriteLine(item.ToString())
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With