Let's say I'm building an itertools.chain
instance as follows:
from itertools import chain
list_1 = list(range(5, 15))
list_2 = list(range(20, 30))
chained = chain(list_1, list_2)
Now, since I already know the length of the lists contained in chained
I can easily get the length of chained
. How can I add the __len__
to chained
?
I tried this:
full_len = len(list_1) + len(list_2)
setattr(chained, '__len__', lambda: full_len)
but it fails with the error
AttributeError: 'itertools.chain' object has no attribute '__len__'
Edit:
I need this to be able to display the progress of a long process with tqdm
, which relays in the __len__
method to be able to show the progress bar