[Solved] Using zip_longest on unequal lists but repeat the last entry instead of returning None

itertools.izip_longest takes an optional fillvalue argument that provides the value that is used after the shorter list has been exhausted. fillvalue defaults to None, giving the behaviour you show in your question, but you can specify a different value to get the behaviour you want: fill = a[-1] if (len(a) < len(b)) else b[-1] for … Read more

[Solved] Need to implement the Python itertools function “chain” in a class

There is not (much) difference between def chain_for(*a): and def __init__(self, *a):. Hence, a very crude way to implement this can be: class chain_for: def __init__(self, *lists): self.lists = iter(lists) self.c = iter(next(self.lists)) def __iter__(self): while True: try: yield next(self.c) except StopIteration: try: self.c = iter(next(self.lists)) except StopIteration: break yield next(self.c) chain = chain_for([1, 2], … Read more

[Solved] Product method in c#

Assuming you mean itertools.product (it looks like it from the example given): public static List< Tuple<T, T> > Product<T>(List<T> a, List<T> b) where T : struct { List<Tuple<T, T>> result = new List<Tuple<T, T>>(); foreach(T t1 in a) { foreach(T t2 in b) result.Add(Tuple.Create<T, T>(t1, t2)); } return result; } n.b. struct here means that … Read more