For dictionary, I can use iter()
for iterating over keys of the dictionary.
y = {\"x\":10, \"y\":20}
for val in iter(y):
print val
I think your actual problem is that you print x
when you mean to print i
iter()
is used to obtain an iterator over a given object. If you have an __iter__
method that defines what iter will actually do. In your case you can only iterate over the counter once. If you defined __iter__
to return a new object it would make it so that you could iterate as many times as you wanted. In your case, Counter is already an iterator which is why it makes sense to return itself.
All of these work fine, except for a typo--you probably mean:
x = Counter(3,8)
for i in x:
print i
rather than
x = Counter(3,8)
for i in x:
print x