Nov-01-2020, 12:16 AM
when using iter() then to get the items from the iterator using list() or tuple() there is a strange difference. here is the demo:
even more strange, the variance of tuple() depends on if the iterator was stored in a variable (b in this case). is there an explanation of why it differs like this or is this a bug? can someone try this in 3.7 or 3.8?
Output:lt2a/phil /home/phil 86> py
Python 3.6.9 (default, Oct 8 2020, 12:12:24)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a=['foo','bar','boo','far','baz']
>>> a
['foo', 'bar', 'boo', 'far', 'baz']
>>> list(x for x in a)
['foo', 'bar', 'boo', 'far', 'baz']
>>> tuple(x for x in a)
('foo', 'bar', 'boo', 'far', 'baz')
>>> list(iter(x for x in a))
['foo', 'bar', 'boo', 'far', 'baz']
>>> tuple(iter(x for x in a))
('foo', 'bar', 'boo', 'far', 'baz')
>>> b=iter(x for x in a)
>>> list(b)
['foo', 'bar', 'boo', 'far', 'baz']
>>> tuple(b)
()
>>>
so, tuple() varies from list().even more strange, the variance of tuple() depends on if the iterator was stored in a variable (b in this case). is there an explanation of why it differs like this or is this a bug? can someone try this in 3.7 or 3.8?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.