Try it here
Subscribe
List Comprehensions uses and advantages

Python List Comprehensions

python_list_comprehensions

A list comprehension is a piece of syntactic sugar that replaces the following pattern:

>>> my_list = []
	 
>>> for i in range(10):
	 my_list.append(i)

	 
>>> my_list
	 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 

With this, equivalent, one:

>>> my_list = [i for i in range(10)]
	 
>>> my_list
	 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 

Advantages of using List Comprehensions

First of all, you’re reducing 3 lines of code into one, which will be instantly recognizable to anyone who understands list comprehensions. Secondly, the second code is faster, as Python will allocate the list’s memory first, before adding the elements to it, instead of having to resize on runtime. It’ll also avoid having to make calls to ‘append’, which may be cheap but add up. Lastly, code using comprehensions is considered more ‘Pythonic’ — better fitting Python’s style guidelines.

Unwrapping a matrix into a vector:

>>> id_matrix = [[1,0,0],
                 [0,1,0],
                 [0,0,1]]
	 
>>> v_matrix = [i for row in id_matrix for i in row]
	 
>>> v_matrix
	 
[1, 0, 0, 0, 1, 0, 0, 0, 1]
>>> 

Filtering a list:

>>> my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
	 
>>> even_num = [i for i in my_list if i % 2 == 0]
	 
>>> even_num
	 
[2, 4, 6, 8]
>>> 

Performance Boost

In order to verify there was an actual boost in performance, below is for-loop version and the list comprehension version of the same code, with and without filtering

import time

BIG = 20000000

def f(k):
    return 2*k

def list_a():
    list_a = []
    for i in range(BIG):
        list_a.append(f(i))

def list_b():
    list_b = [f(i) for i in range(BIG)]

def list_a_filtered():
    list_a = []
    for i in range(BIG):
        if i%2==0:
            list_a.append(f(i))

def list_b_filtered():
    list_b = [f(i) for i in range(BIG) if i%2==0]

def benchmark(function, function_name):
    start = time.time()
    function()
    end = time.time()
    print("{0} seconds for {1}".format((end - start), function_name))

benchmark(list_a, "list a")
benchmark(list_b, "list b")
benchmark(list_a_filtered, "filtered list a")
benchmark(list_b_filtered, "filtered list b")

The list_a methods generate lists the usual way, with a for-loop and appending. The list_b methods use List Comprehensions.

Below is the result:

  • 5.049126863479614 seconds for list a
  • 4.051025629043579 seconds for list b
  • 4.466981410980225 seconds for filtered list a
  • 4.0249669551849365 seconds for filtered list b

We see a 20% boost in speed from switching to List Comprehensions in the unfiltered case, whereas the filtered algorithm only gets a 10% boost. This corroborates our theory that the main performance advantage comes from not having to call the append method at each iteration, which is skipped on every other iteration in the filtered case.

Writer profile pic

Nupur on Sep 28, 2020 at 12:09 am


This article is contributed by Nupur. If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.



Post Comment

Comments( 0)

×

Forgot Password

Please enter your email address below and we will send you information to change your password.