An object that can directly act on a for loop is called an iterative object (iterable).
An object that can be called by the next () function and continually returns the next value is called an iterator (Iterator).
All iterable can be converted to iterator via the built-in function iter ().
Iterators provide an interface for class sequence objects with a sequence of classes. Python's iterations seamlessly support sequence objects, and it also allows programmers to iterate over non-sequential types, including user-defined objects. Iterators are handy, you can iterate over objects that are not sequences but behave in sequence, such as dictionary keys, rows of a file, and so on. The role of iterators is as follows:
>>> i=iter (' ABCD ') >>> print i.next () a>>> print i.next () B>>>print i.next () c
s = {' One ': 1, ' One ': 2, ' three ': 3}print s{' three ': 3, ' both ': 2, ' one ': 1}>>> m = iter (s) >>> print m.next () t hree>>> print m.next () two>>> print m.next () one
dict {' key ': value, ' key ': value}
The query can be queried either by the Get method or by key directly, but if the key does not exist, the error will be given, and get will give none
You can add or change dict,dict (' key ') directly =value
You can use the Pop method Dict.pop (Key[,default]) to delete the elements within the dict with the key value and return the value corresponding to the deleted key. If key does not exist and the default value is not set, the Keyerror exception is returned
You can use the Clear Method Dict.clear () to empty the Dict
VALUES ()/Itervalues () method: Returns the value of Dict,
Replace the values () method with the itervalues () method, which is exactly the same as the iteration effect. The itervalues () method does not convert, and it takes the value from the dict in turn during the iteration,
So the Itervalues () method saves the memory needed to generate a list than the values () method.
Items () method
>>> s = {' One ': 1, ' both ': 2, ' three ':3}>>> d = items (s) >>> d=s.items () >>> print d[(' Three ', 3), (' One ', 2), (' One ', 1)]
and values () have a itervalues () like, items () also have a corresponding iteritems (),
Iteritems () does not convert the dict to list, but the tuple is given continuously during the iteration, so iteritems () does not occupy additional memory
Python iter, iterator &dict, dictionary explanation