List Comprehensions in Python — Explained

栏目: IT技术 · 发布时间: 4年前

内容简介:Thank you for reading. Please let me know if you have any feedback.

List Comprehensions in Python — Explained

When to use and not to use list comprehensions.

List Comprehensions in Python — Explained

Photo by Hilthart Pedersen on Unsplash

Listis a built-in data structure in Python and a collection of data points in square brackets. Other built-in data structures in Python are set , tuple , and dictionary .

list_a = [4, 'data', 3.2, True]list_a
[4, 'data', 3.2, True]

In this post, we will cover list comprehensions in python and their advantages. Although list comprehensions are highly practical, there are cases in which list comprehension is not the optimal choice. We will also go through situations when not to use list comprehensions.

List comprehension is basically creating lists based on existing lists. Following is a list comprehension that creates a list based on the lenghts of words in another list.

words = ['data', 'science', 'machine', 'learning']word_length = [len(i) for i in words]word_length
[4, 7, 7, 8]

The basic syntax for the list comprehension is:

Python list comprehension

In the previous example, expression is len(i), item is the elements in “words” list represented by “i”. The iterable is, of course, the “words” list. We did not have a conditional statement but let’s do another one with a condition. For instance, the following list comprehension creates a list with the words with a length greater than 5.

words = ['data', 'science', 'machine', 'learning']long_words = [i for i in words if len(i) > 5]long_words
['science', 'machine', 'learning']

Expression can be any expression that returns a value. Iterable can be any object that can iteratively return its elements such as a list, set, generator.

Conditionals are critical because they allow to filter out values or select only what we need.

For instance, the following piece of code creates a list consists of the squares of even numbers in range(20).

even_squares = [i*i for i in range(20) if i%2 == 0]even_squares
[0, 4, 16, 36, 64, 100, 144, 196, 256, 324]

We can create a list with the maximum numbers of rows of a matrix.

#Create a matrix
import numpy as np
A = np.random.randint(10, size=(4,4))
A
array([[1, 7, 4, 4],        
       [5, 0, 0, 6],        
       [7, 5, 8, 4],        
       [1, 3, 2, 2]])#select the max of rows
max_element = [max(i) for i in A]
max_element
[7, 6, 8, 3]

We can also put a condition on the expression. The following piece of code iterates over “words” list. It gets the words if the length is greater than 7. It writes “short word” instead of the elements whose length is not greater than 7.

words = ['data', 'science', 'artificial', 'intelligence', 'machine', 'learning']long_words = [i if len(i) > 7 else 'short word' for i in words]print(long_words)
['short word', 'short word', 'artificial', 'intelligence', 'short word', 'learning']

List comprehension vs for loop vs map

What we do with list comprehensions can also be done with for loops or map function. Let’s do the first example using both for loop and map function:

#for loop
word_length = []
for word in words:
   word_length.append(len(word))word_length
[4, 7, 7, 8]#map function
word_length = list(map(lambda x: len(x), words))word_length
[4, 7, 7, 8]

The advantage to use list comprehensions:

  • They are relatively faster than for loops.
  • They are considered to be more pythonic than for loop and map function.
  • The syntax of list comprehension is easier to read and understand.

Let’s do a comparison by creating a list with the squares of first 50000 integers:

List Comprehensions in Python — Explained

As we can see, the list comprehension is the fastest.

Note: Every list comprehension can be written using a for loop but not every for loop can be represented with a list comprehension.

When not to use list comprehension

List comprehension loads the entire output list into memory. This is acceptable or even desirable for small or medium-sized lists because it makes the operation faster. However, when we are working with large lists (e.g. 1 billion elements), list comprehension should be avoided. It may cause your computer to crash due to the extreme amount of memory requirement.

A better alternative for such large lists is using a generator which does not actually create a large data structure in memory. A generator creates items when they are used. After the items are used, generator throws them away. Using a generator, we can ask for the next item in an iterable until we reach the end and store a single value at a time.

The following generator sums the squares of first 10 million integers.

sum(i*i for i in range(10000000))333333283333335000000

Map function does not also cause a memory problem.

sum(map(lambda i: i*i, range(10000000)))333333283333335000000

There is no free lunch! Generators or map functions do not cause a memory issue but they’re relatively slower than list comprehensions. Similarly, the speed of list comprehensions comes from the excessive memory usage. You can decide on which one to use depending on your application.

Thank you for reading. Please let me know if you have any feedback.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Ajax实战

Ajax实战

Dave Crane Eric Pascarello / 李锟(网名dlee) / 人民邮电出版社 / 2006年4月 / 69

本书是目前 Ajax 领域最为全面深入的一本著作,其中不仅有对于基础知识的介绍,还有对于 Ajax 开发中重大的体系架构问题的深入探讨,总结了大量 Ajax 开发中的设计模式,并讨论了框架、安全性与性能等等。书中提供了几个典型的例子,兼顾各种开发平台,这些例子的代码稍作修改就可以直接应用于项目开发之中,代码源文件可以从图灵网站下载。本书内容广泛且深入,同时适用于各个层次的 Web 应用开发人员。一起来看看 《Ajax实战》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试