[PYTHON] Not just list comprehension

I thought that the list comprehension could only be used for the list because the list comprehension appeared in the study materials, but I found that it could be used in various places, so I summarized it. In English, it's list comprehension, so I feel like I can use it elsewhere.

~ %python3
Python 3.4.0 (v3.4.0:04f714765c13, Mar 15 2014, 23:02:41) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from itertools import tee
>>> Generator = (i for i in range(10))  #Generator comprehension
>>> g = tee(Generator, 5)  #5 generators
>>> Str = "comprehension"
>>> List = [g[0]]  #List comprehension
>>> Tuple = tuple(g[1])  #Tuple inclusion notation
>>> Dict = {k:v for (k,v) in zip(Str, g[2])}  #Dictionary comprehension dict(zip(Str, g[2]))But the same
>>> Set = set(s for s in Str)  #Set comprehension set(Str)But the same
>>> g
(<itertools._tee object at 0x102a6ab08>, <itertools._tee object at 0x102a6aac8>, <itertools._tee object at 0x102a6ab48>, <itertools._tee object at 0x102a6ab88>, <itertools._tee object at 0x102a6af08>)
>>> List
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> Tuple
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> Dict
{'c': 0, 'p': 3, 'm': 2, 'h': 6, 'r': 4, 'e': 7, 'o': 1, 'n': 8, 's': 9}
>>> Set
{'p', 's', 'e', 'c', 'h', 'm', 'r', 'o', 'n', 'i'}
>>> sum(g[3])  #Sum
45
>>> g[4]
<itertools._tee object at 0x102a6ab88>
>>> while g[4]:
...     next(g[4])
... 
0
1
2
3
4
5
6
7
8
9
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
StopIteration

Recommended Posts

Not just list comprehension
List comprehension
List comprehension
Python> Comprehension / Comprehension> List comprehension
Note: List comprehension
Python Exercise 2 --List Comprehension
FizzBuzz in list comprehension
Python list comprehension speed
Quicksort 2 | Easy list comprehension
Python list is not a list
[Python] Not just count collections.Counter
Judgment of if by list comprehension
Python basic operation 1st: List comprehension notation
Python comprehension (list and generator expressions) [additional]