2

Consider these 3 short snippets:

Snippet 1:

nation = ['Roman', 'Egypt', 'Greek', 'Chinese', 'Islamic', 'Mayan', 'Persian', 'Mongol']
golden_age = ['27BC-1453AD', '3150BC-30BC', '800BC-600AD', '221BC-1912AD', '750AD-1257AD', '2000BC-1540AD', '550BC-651AD', '1206AD-1368AD']
a = zip(nation, golden_age)
print(a)

Snippet 2:

b = list(a)
print('b',b)

Snippet 3:

c = tuple(a)
print('c',c)

Suppose at first I run snippet 1. Then the second and the third. Amazingly the third snippet prints c() (It seems that the object a is gone after I passed it to the list constructor in snippet 2. So when converting it to tuple the result is null!)

Now change the run order to 1 -> 3 -> 2. This time, the second snippet prints null list! I looks like that the tuple constructor has eaten the object a.

WHY tuple should affect its input argument? (also for list and maybe others!)

Saleh
  • 1,819
  • 1
  • 17
  • 44
  • 4
    That's because `zip` is a generator in python 3. Use the same method e.g. `list(a)` 3 times in a row and see the behaviour. – roganjosh Jun 02 '18 at 11:23

0 Answers0