Python List Comprehension Command

From GM-RKB
(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

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: ...