I was reading through some older code of mine and came across this line
itertools.starmap(lambda x,y: x + (y,),
itertools.izip(itertools.repeat(some_tuple,
len(list_of_tuples)),
itertools.imap(lambda x: x[0],
list_of_tuples)))
To be clear, I have some list_of_tuples
from which I want to get the first item out of each tuple (the itertools.imap
), I have another tuple that I want to repeat (itertools.repeat
) such that there is a copy for each tuple in list_of_tuples
, and then I want to get new, longer tuples based on the items from list_of_tuples
(itertools.starmap
).
For example, suppose some_tuple = (1, 2, 3)
and list_of_tuples = [(1, other_info), (5, other), (8, 12)]
. I want something like [(1, 2, 3, 1), (1, 2, 3, 5), (1, 2, 3, 8)]
. This isn't the exact IO (it uses some pretty irrelevant and complex classes) and my actual lists and tuples are very big.
Is there a point to nesting the iterators like this? It seems to me like each function from itertools would have to iterate over the iterator I gave it and store the information from it somewhere, meaning that there is no benefit to putting the other iterators inside of starmap
. Am I just completely wrong? How does this work?