Here is the sample code1 and its result.
d ={
"name":"looser",
"roll":6666,
"profile" : "developer"
}
print("list formatted dict is {}".format([d]))
for k,v in [d.items()]:
if d[k] is "developer":
d.pop(k)
else:
print(k)
result:
list formatted dict is [{'name': 'looser', 'roll': 6666, 'profile': 'developer'}]
Traceback (most recent call last):
File "/Users/rough/try.py", line 18, in <module>
for k,v in [d.items()]:
ValueError: too many values to unpack (expected 2)
Process finished with exit code 1
Here is the sample code2 with modified form.
d ={
"name":"looser",
"roll":6666,
"profile" : "developer"
}
print("list formatted dict is {}".format([d]))
for k,v in list(d.items()):
if d[k] is "developer":
d.pop(k)
else:
print(k)
result:
list formatted dict is [{'name': 'looser', 'roll': 6666, 'profile': 'developer'}]
name
roll
Process finished with exit code 0
On many places people tell me there is no difference, is it true there is no difference.If there is no difference then why such result.
Because one is solving my problem of **RuntimeError: dictionary changed size during iteration **