I’m learning on Python 3.8 (32-bit) on a Windows-10 machine.

 

In Section 4.2. for Statements, the second example “Iterate over a copy” fails in practice.

 

>>> for user, status in users.copy().items():

...     if status == 'inactive':

...             del users[user]

...

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

NameError: name 'users' is not defined

>>> 

>>> active_users = {}

>>> for user, status in users.items():

...     if status == 'active':

...             active_users[user] = status

...

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

NameError: name 'users' is not defined

>>>