(Apr-17-2019, 12:35 AM)Drone4four Wrote: I’m not sure I understand what you mean when you say “consume the iterator”. You explained that print(list(flat_list))
wouldn’t work because the last was supposed to be already exhausted. But I entered each line into my Python interpreter and there was no traceback or issue presented.
There is subtle difference in here:
In [8]: from itertools import chain In [9]: flat_list = chain.from_iterable(['apples', 'bananas', 'oranges']) In [10]: print(flat_list) # printing out object, not consuming iterator <itertools.chain object at 0x10cd56048> In [11]: list(flat_list) # consuming iterator i.e. iterating over and creating list Out[11]: ['a', /../ 's'] In [12]: list(flat_list) # consuming exhausted iterator Out[12]: []
(Apr-15-2019, 02:35 AM)Drone4four Wrote: I’m trying to get slice a list of strings into individual characters ...but using slicing.
I observed that there is no answer to original question. I have no idea why would anyone want to do it as there are built-in tools for that. However, following silly code uses slicing as aftertought to get list of individual characters:
>>> lst = ['apples', 'bananas', 'oranges'] >>> [fruit[i:i+1] for fruit in lst for i in range(len(fruit))]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.