dict.popitem()
is not the same thing as dict.iteritems()
; it removes one pair from the dictionary as a tuple, and you are looping over that pair.
The most efficient method is to use a while
loop instead; no need to call len()
, just test against the dictionary itself, an empty dictionary is considered false:
while d:
key, value = d.popitem()
print key, value
The alternative is to use reversed()
:
for key, item in reversed(d.items()):
print key, value
but that requires the whole dictionary to be copied into a list first.
However, if you were looking for a FIFO queue, use collections.deque()
instead:
from collections import deque
d = deque(["a0.csf", "b1.csf", "c2.csf"])
while d:
item = d.pop()
or use deque.reverse()
.