Title: After changing to list or tuple, will the original parameter change?
Type: Stage: resolved
Components: Versions:
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: foxpython_2020, steven.daprano
Priority: normal Keywords:

Created on 2020-10-10 23:54 by foxpython_2020, last changed 2020-10-11 02:07 by steven.daprano. This issue is now closed.

Messages (2)
msg378417 - (view) Author: foxpython_2020 (foxpython_2020) Date: 2020-10-10 23:54
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
print("1:",c) # right: {'one': 1, 'two': 2, 'three': 3}

b= zip(['one', 'two', 'three'], [1, 2, 3])
a= list(b) #Because of this line of code, the result is different
print("2:",dict(b)) #wrong: {}

b= zip(['one', 'two', 'three'], [1, 2, 3])
a= tuple(b) #Because of this line of code, the result is different
print("2:",dict(b)) #wrong: {}
msg378419 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-10-11 02:07
This is not a bug. In Python 2, zip() returns a list, so you can use it over and over again. But in Python 3, zip() returns an iterator, and you can only use iterators once. After you have used the iterator, it is exhausted and there is nothing left.

So the behaviour you see is expected and intentional.
Date User Action Args
2020-10-11 02:07:20steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg378419

resolution: not a bug
stage: resolved
2020-10-10 23:54:23foxpython_2020create