Utilities
Key 3: Easy iterations by comprehensions.
We have various syntax and utilities to iterate efficiently over iterators. Comprehensions work on iterator and provide results as another iterator. They are implemented in native C, and hence, they are faster than for loops.
We have list, dictionary, and set comprehensions, which produce list, dictionary, and set as result, respectively. Also, iterators avoid declaring extra variables that we need in a loop:
>>> ll = [ i+1 for i in range(10)] >>> print(type(ll),ll) <class 'list'> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> ld = { i:'val'+str(i) for i in range(10) } >>> print(type(ld),ld) <class 'dict'> {0: 'val0', 1: 'val1', 2: 'val2', 3: 'val3', 4: 'val4', 5: 'val5', 6: 'val6', 7: 'val7', 8: 'val8', 9: 'val9'} >>> ls = {i for i in range(10)} >>> print...