I'm trying to better frame little variations over itertools.chain()
. Let's say we have two lists:
l1 = [1,2,3]
l2 = [4,5,6,7,8,'a','b','c']
By simply doing
list(itertools.chain(l1,l2))
Out[464]: [1, 2, 3, 4, 5, 6, 7, 8, 'a', 'b', 'c']
it will merge the two iterators into a single one. What I've often seen people doing is also
list(itertools.chain(*(l1,l2)))
Out[465]: [1, 2, 3, 4, 5, 6, 7, 8, 'a', 'b', 'c']
which returns the same result. Which exactly is the role of asterisk *
on this one?
Lastly, if I run the same code but without the asterisk (but with double parentheses), then I get
list(itertools.chain((l1,l2)))
Out[466]: [[1, 2, 3], [4, 5, 6, 7, 8, 'a', 'b', 'c']]
So the original two lists are kept separated and put into a bigger one. What differences are there between these options?
EDIT:
I'm adding details to my question by inserting the specific usage I was referring to (cross-validation):
#k=0,1,2.
for k in range(0, n_folds):
print('---------------------- Fold [{}/{}] ----------------------'.format(k + 1, n_folds))
# load training dataset
dataset_train = list(itertools.chain(*(k_folds[:k] + k_folds[k + 1:])))
k_folds
here is just a list containing three separate folds of data.
What's the point here in using or not the asterisk? In light of the comment of @wjandrea, I don't see any here.
What it results is this:
itertools.chain(k_folds + k_folds[1]) == itertools.chain(*(k_folds + k_folds[1]))
Out[646]: False
So clearly not the same in this context but I don't know why...