What Are The Uses Of List Comprehensions Feature Of Python

What Are The Uses Of List Comprehensions Feature Of Python


  • 14 Feb, 2013

    List comprehension is another very powerful feature, saving a lot of time ! (that would have been wasted writing loops).

     

    >>> q
    [1, 2, 3, 4]
    >>> z = [i**2 for i in q]
    >>> z
    [1, 4, 9, 16]
    >>>

    map is an utility which applies a function to all the elements of the list. 

    >>> def sq(x):
    ... return x*x
    ...
    >>> t=map(sq, q)
    >>> t
    [1, 4, 9, 16]
    >>>

    Comments Received:
    Please give your suggestions and feedback:
  • 23 Dec, 2011

    List comprehensions help to create and manage lists in a simpler and clearer way than using map(), filter() and lambda. Each list comprehension consists of an expression followed by a clause, then zero or more for or if clauses.

    Comments Received:
    Please give your suggestions and feedback:

2009-2016 downloadmela.com. All rights reserved.