Python Forum
strange difference between list() and tuple() using iter()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
strange difference between list() and tuple() using iter()
#1
when using iter() then to get the items from the iterator using list() or tuple() there is a strange difference. here is the demo:
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.
Reply


Messages In This Thread
strange difference between list() and tuple() using iter() - by Skaperen - Nov-01-2020, 12:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What is the difference between list and tuples in Python? priya1717 1 25,807 Dec-20-2022, 11:08 AM
Last Post: ibreeden
  why tuple instead of list Skaperen 12 5,951 Feb-19-2020, 02:07 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020