Python List Comprehension Command
(Redirected from Python List Comprehension)
Jump to navigation
Jump to search
A Python List Comprehension Command is a Python list operation that … on Python lists.
- Example(s):
>>> squares = [x**2 for x in range(10)] ;
print squares ;
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
- See: Python Command, Scala List, Perl List.
References
2017
- https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
- QUOTE: List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
2015
- http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/
- QUOTE: … List comprehensions are a tool for transforming one list (any iterable actually) into another list. During this transformation, elements can be conditionally included in the new list and each element can be transformed as needed.
If you’re familiar with functional programming, you can think of list comprehensions as syntactic sugar for a filter followed by a map: ...
- QUOTE: … List comprehensions are a tool for transforming one list (any iterable actually) into another list. During this transformation, elements can be conditionally included in the new list and each element can be transformed as needed.